Front-end framework: state
A component's local state and the render that follows, derived state, lifting state to the common ancestor, the shared store, and updating without changing in place.
- 25 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Component state: what is it?
Show the answer
A piece of data a component keeps from one render to the next and whose change triggers a new display.
An ordinary variable inside a function is recreated on every render; state is kept by the framework.
The counter is 0 and the button is clicked once. What is
non the next render?
```jsx
const [n, setN] = useState(0);
function clic() {
setN(n + 1);
setN(n + 1);
}`1203
Show the answer
1During this render,
nis 0 throughout the function: both calls ask “set 1”. To chain them, give a function that starts from the latest value:setN((v) => v + 1)twice gives 2.In React,
useState(0)returns a pair: the state's value and an …, whose call replaces the value and requests a ….Show the answer
In React,
useState(0)returns a pair: the state's value and an update function, whose call replaces the value and requests a new render.Writing
n = 5directly changes nothing on screen: only calling the update function tells the framework.Reactivity: what is it?
Show the answer
The property of an interface that redraws itself when the data it depends on changes.
You do not change the screen: you change the state, and the framework recomputes the affected components.
After clicking “Add”, the screen does not change although
liste.push(x)does run. Which fix works?setListe([...liste, x])liste.push(x); setListe(liste)liste.push(x)inside auseEffectwithout dependenciesliste = [...liste, x]
Show the answer
setListe([...liste, x])pushchanges the array in place, andsetListe(liste)hands back the same reference: the framework sees no change. Reassigningliste(a constant) tells nobody. You need a new array passed to the update function.