Coding task · JavaScript and React course · Module 3: State and Events

Practice: Lifting State Up

Lifting state up: when two components need the same data, their common parent holds the state. Data flows down through props, and changes go back up through a function passed in props (onPowerChange).

Task

Create a parent ColonyControl with a powerLevel state (useState(50)) and two children without their own state: PowerPlant({ powerLevel, onPowerChange }) shows the power level and has +10 and -10 buttons that call onPowerChange with the new value, and LifeSupport({ powerLevel }), which shows 'Life support: active' when powerLevel >= 30 and 'ALARM: not enough power' otherwise. ColonyControl passes the same powerLevel to both children. Render ColonyControl in App.