Conditional Rendering: The Traffic Light
Mission
Show different UI depending on state, using clean techniques: a lookup object, ternaries and the && shortcut.
Tasks
- 1
Create a state called
status, initialised to'loading'. - 2
Define a config object outside the return that maps each status (
success,error,loading) to a color, an icon and a message. - 3
Make the container's background use the color of the current status.
- 4
Render the icon and the message by reading from your config object.
- 5
Wire the three buttons so each one sets the matching status.
import React, { useState } from 'react'; export default function TrafficLight() { // 1. Create the 'status' state (starts as 'loading') // 2. Define your CONFIGS object return ( <div style={{ padding: '20px', fontFamily: 'sans-serif', textAlign: 'center' }}> <h1>System Status</h1> {/* 3. The container background must change with the status */} <div style={{ padding: '30px', borderRadius: '12px', marginTop: '20px', transition: 'all 0.3s ease' }}> {/* 4. Show the icon and the message here */} </div> {/* 5. Wire the buttons so they change the status */} <div style={{ marginTop: '20px', display: 'flex', gap: '10px', justifyContent: 'center' }}> <button>Success</button> <button>Error</button> <button>Loading</button> </div> </div> ); }
Requirements
0/6Has a `status` state that starts as 'loading' (not run yet)
Defines a config object with success, error and loading (not run yet)
Reads the config with CONFIGS[status] (not run yet)
No switch, if/else chains or nested ternaries (not run yet)
The container's background is dynamic (not run yet)
Each button sets success, error or loading (not run yet)
AI mentor
Reviews only the files you changed, plus the checks you missed.
Run the checks first. They are free and instant.