Show different JSX for a signed-out and a signed-in visitor.
What to build
A status bar that changes with the user.
Requirements
- Write
Status({ user }). Whenuseris null, return adivwith the classstatusholding abuttonSign in. - Otherwise return a
divwith the classstatusholding apSigned in as NAMEand abuttonSign out. ApprendersStatustwice: withuser={null}and withuser="Mitu".
Constraints
Use an early return or the conditional operator.
Example
The first bar has only a Sign in button; the second says "Signed in as Mitu" with a Sign out button.
Why it works
The same component chooses its JSX from its props, so each place it is used shows the right state.
Hints
Hint 1
if (!user) return ...; then the signed-in JSX.
Hint 2
App renders the component twice, with user={null} and with a name.
Solution
Show one correct solution (try it yourself first)
function Status({ user }) {
if (!user) {
return (
<div className="status">
<button>Sign in</button>
</div>
);
}
return (
<div className="status">
<p>Signed in as {user}</p>
<button>Sign out</button>
</div>
);
}
export default function App() {
return (
<div>
<Status user={null} />
<Status user="Mitu" />
</div>
);
}