In React/JSX, here is a common pattern:
{name && <Header />}
name is a state variable and <Header />
is a component. If name is not null, than the Header is displayed. If name is null (for example, it hasn’t been entered by the user), then nothing is displayed. This works because the && expression only evaluates to true if both elements are true.
{name === "Mike" ? <Header headerName="Mike"/> : <Header headerName="Some silly name"/>
This one uses the ternary operator to control what’s displayed. If the variable, name, has the value of “Mike,” then <Header is displayed with the headerName (a property I made up) of “Mike”, otherwise with the value of “Some silly name”.
<input type="text" onChange={(e) => setName(e.target.value)}/>
Here we have a text input. When you type something in it, that calls the onChange prop, which in this case is set to a callback function that calls the setName function (this is a useState hook) that will give you the value of the input. You can get this because the onChange event will give you the target.value property, which is whatever’s written in the input.
Anyone else have any React tricks? I feel like I only have like four or five of them and I keep trying to use them for everything. I want to add to my arsenal!