Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of conditional rendering in React, including if statements, ternary operators, logical AND, conditional components, and rendering based on state and props.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. Rendering different UI based on conditions
Conditional rendering allows React components to display different content depending on conditions such as state or props.
A. if
An if statement can be used to determine what JSX or component should be returned.
A. Ternary operator
The ternary operator uses the form condition ? expressionIfTrue : expressionIfFalse and is commonly used inside JSX.
A. {isLoggedIn ? 'Welcome' : 'Please log in'}
The ternary operator is appropriate when one of two different values or UI elements should be rendered.
A. &&
The logical AND operator can conditionally render JSX when the expression on its left evaluates to a truthy value.
B. Renders AdminPanel when isAdmin is truthy
With logical AND, the right-hand JSX expression is evaluated and rendered when isAdmin is truthy.
A. An if statement with different return statements
An if statement can be used before the main return to return different JSX for different conditions.
A. To provide a fallback when a value is null or undefined
The ?? operator returns the right-hand value when the left-hand value is null or undefined.
A. {loading ? <Spinner /> : <Content />}
The ternary operator can be used directly inside JSX to choose between two UI elements.
A. Return null
Returning null from a React component means that the component renders no UI.
A. State and props
Components commonly use their state and props as inputs for determining which UI should be rendered.
A. React may render 0
Unlike false, null, and undefined, the number 0 can be rendered by React. This can cause an unexpected 0 to appear when using count && JSX.
A. if/else if/else
if/else if/else is useful when several mutually exclusive conditions determine which UI should be rendered.
A. The expression after ? is selected
In condition ? valueIfTrue : valueIfFalse, the valueIfTrue expression is selected when the condition is truthy.
A. React uses normal JavaScript expressions and control flow to determine what UI to render
React relies on JavaScript constructs such as if statements, ternary expressions, and logical operators for conditional rendering.