I am reading a book "Pure React" by Dave Ceddia, below is a question I am getting stuck, I appreciate for any solution as simple as possible, thanks.
Question: Return the appropriate JSX from this component so that when username is undefined or null, it renders “Not logged in”. When username is a string, render “Hello, username
My answer:
import React from 'react';
import ReactDOM, { render } from 'react-dom';
function Greeting(){
render(){
let username = '';
let greeting = '';
if(typeof username == String){
greeting = <span>Hello {username}</span>
}else{
greeting = <span>Not logged in</span>;
}
return (
<div>
<input type="text" value={username}/>
{greeting}
</div>
)
}
}
ReactDOM.render(
<Greeting/>,
document.getElementById('root')
);