Conditional rendering
You can use a conditional statement to render different UIs if a condition is true or false. This feature can be used, for example, to show or hide some elements, handle authentication, and so on.
In the following example, we will check if props.isLoggedin is true. If so, we will render the <Logout /> component; otherwise, we render the <Login /> component. This is now implemented using two separate return statements:
function MyComponent(props) {
const isLoggedin = props.isLoggedin;
if (isLoggedin) {
return (
<Logout />
)
}
return (
<Login />
)
}
You can also implement this by using condition ? true : false logical operators, and then you need only one return statement, as illustrated here:
function MyComponent(props) {
const isLoggedin = props.isLoggedin;
return (
<>
{ isLoggedin ? <Logout /> : <Login /> }
</>
);
}