Coding task · JavaScript and React course · Module 3: State and Events
Practice: Toggle Switch
Boolean state: const [isOn, setIsOn] = useState(false). Toggling: onClick={() => setIsOn(prev => !prev)} - !prev inverts the value. Set the colour conditionally, e.g. style={{ color: isOn ? 'green' : 'red' }}.
Task
Create 3 boolean states: [lifeSupport, setLifeSupport] = useState(true), [powerGrid, setPowerGrid] = useState(true) and [communication, setCommunication] = useState(false). For each system add a button that toggles its state (prev => !prev) and display the status, e.g. Life Support: {lifeSupport ? 'ON' : 'OFF'}. The status colour depends on the state: green for ON, red for OFF.