AceDevHub
Beginner React Interview QuestionsBeginnerPractical

React · Question 16

How does conditional rendering work in React?

Direct answer

React uses normal JavaScript control flow such as if statements, ternaries, logical &&, and returning null to decide which JSX should be rendered.

Status.jsx
function Status({ isLoggedIn }) {
  if (!isLoggedIn) {
    return <LoginButton />;
  }

  return <Dashboard />;
}
  • if / early return: useful when whole branches differ.
  • condition ? A : B: useful for choosing between two JSX expressions.
  • condition && A: useful when an element is optional.
  • return null: render nothing from that component.