The double ampersand(&&
) means the logical AND operator.
The code you've provided uses short-circuit evaluation to determine whether to render the button
or not. To simplify the code if auth.loading
is false and user === auth.user._id
is true then the button
will be rendered.
More details on short-circuit evaluation with logical AND operator
expression 1 && expression 2
In the case above if expression 1
is falsy expression 2
will not be evaluated, because whether the expression 2
evaluates to true of false the result of the combined expression will be false
because both the expression needs to be true for the combined expression to evaluate to true.
Example,
const funcOne = () => { console.log('called funcOne'); return false; }
const funcTwo = () => { console.log('called funcTwo'); return true; }
console.log( funcOne() && funcTwo() );
// in the case above funcTwo will not be called because
// the firstpart of the expression funcOne() && funcTwo()
// evaluates to false (the return value of funcOne is false)
The usage of short-circuit evaluation with React
Keep in mind JSX
is just syntactic sugar for function call, <button>Hello</button>
will be converted to a React.createElement
function call behinds the scene. That is why it is nothing different to render JSX
with short-circuit evaluation than using short-circuit evaluation with regular function calls.
expr1 && expr2 && <button>Hello</button>
// will be converted to
expr1 && expr2 && React.createElement("button", null, "Hello");
// so if expr1 or expr2 evaluate to false React.createElement will not be -
// called, both expr1 and expr2 have to evaluate to true for the-
// React.createElement function to be called