0
0
Reactframework~10 mins

If conditions in JSX in React - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If conditions in JSX
Start Render
Evaluate Condition
Render JSX
Update DOM
React checks the condition during render. If true, it shows one JSX part; if false, it shows another or nothing.
Execution Sample
React
function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <p>Welcome back!</p> : <p>Please sign in.</p>}
    </div>
  );
}
This component shows a welcome message if logged in, else asks to sign in.
Execution Table
StepisLoggedInCondition (isLoggedIn ?)Rendered JSXDOM Update
1truetrue<p>Welcome back!</p>Shows 'Welcome back!' message
2falsefalse<p>Please sign in.</p>Shows 'Please sign in.' message
3nullfalse<p>Please sign in.</p>Shows 'Please sign in.' message
4undefinedfalse<p>Please sign in.</p>Shows 'Please sign in.' message
50false<p>Please sign in.</p>Shows 'Please sign in.' message
6''false<p>Please sign in.</p>Shows 'Please sign in.' message
💡 Rendering ends after JSX is chosen based on condition.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6
isLoggedInundefinedtruefalsenullundefined0''
Key Moments - 2 Insights
Why does the component show 'Please sign in.' when isLoggedIn is null or undefined?
Because in the execution_table rows 3 and 4, the condition evaluates to false for null and undefined, so the else JSX is rendered.
What happens if isLoggedIn is 0 or an empty string?
As shown in rows 5 and 6, 0 and '' are treated as false in the condition, so the else JSX is shown.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what JSX is rendered when isLoggedIn is true?
A<p>Please sign in.</p>
BNothing is rendered
C<p>Welcome back!</p>
DAn error occurs
💡 Hint
Check row 1 in the execution_table where isLoggedIn is true.
At which step does the condition become false for the first time?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the Condition column in execution_table rows.
If isLoggedIn was changed to a non-empty string, how would the rendered JSX change?
AIt would render <p>Welcome back!</p>
BIt would render <p>Please sign in.</p>
CIt would render nothing
DIt would cause an error
💡 Hint
Non-empty strings are truthy in JavaScript, so condition becomes true.
Concept Snapshot
JSX if conditions use JavaScript expressions inside braces.
Use ternary: condition ? JSX if true : JSX if false.
Falsy values (false, 0, '', null, undefined) render the else part.
Truthy values render the true part.
This controls what React shows on screen.
Full Transcript
In React JSX, you can use if conditions by writing JavaScript expressions inside curly braces. The most common way is using the ternary operator: condition ? true JSX : false JSX. React evaluates the condition during rendering. If the condition is true, it shows the first JSX; if false, it shows the second. For example, if isLoggedIn is true, it shows 'Welcome back!'. If false or other falsy values like null or empty string, it shows 'Please sign in.'. This lets you control what the user sees based on state or props. Remember, JavaScript truthy and falsy rules apply here.