Are the { }
to indicate jsx inside the html?
The curly brackets are what allow you to "inject" javascript into the HTML, i.e. in this case the javascript logic for using the ternary operator to conditionally render one thing or another, <div>{condition ? "this thing" : "the other thing"}</div>
. {}
are used when injecting values into the JSX, i.e. <div>Value: {state.value}</div>
.
What is the ) : (
for?
This is part of a larger syntax, specifically ( .... ) : ( .... )
which is yet part of a larger ternary operator syntax, ... ? ( .... ) : ( .... )
.
The parenthesis are mostly optional, but can help with making the code more readable (which is subjective enough already).
Compare/contrast the following examples:
{pathnames.length ? (
<Link onClick={() => navigate("/")}>Home</Link>
) : (
<Typography>Home</Typography>
)}
{pathnames.length ? <Link onClick={() => navigate("/")}>Home</Link> : <Typography>Home</Typography>}
{pathnames.length
? <Link onClick={() => navigate("/")}>Home</Link>
: <Typography>Home</Typography>
}
All are valid and work. What formatting you choose is mostly up to you and what is easiest to read and maintain. I usually set my linters and code prettiers to auto-magically format to the first example.