0
0
Reactframework~10 mins

State re-render behavior in React - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to update state and trigger a re-render in a React functional component.

React
const [count, setCount] = useState(0);

function increment() {
  setCount([1]);
}
Drag options to blanks, or click blank then click option'
Acount + 1
Bcount - 1
C0
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same value without change, so no re-render happens.
Decreasing the count instead of increasing it.
2fill in blank
medium

Complete the code to correctly read the current state value inside a functional component.

React
const [name, setName] = useState('');

return <p>Hello, [1]!</p>;
Drag options to blanks, or click blank then click option'
AName
Bname
CsetName
DuseState
Attempts:
3 left
💡 Hint
Common Mistakes
Using the setter function instead of the state value.
Using a capitalized variable that does not exist.
3fill in blank
hard

Fix the error in the code to properly update state based on the previous state value.

React
setCount([1] => [1] + 1);
Drag options to blanks, or click blank then click option'
Aprev
Bcount
Cstate
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the state variable directly inside the updater function.
Using a variable name that is not defined.
4fill in blank
hard

Fill both blanks to create a state object and update one property without losing others.

React
const [user, setUser] = useState({ name: '', age: 0 });

setUser({ ...user, [1]: [2] });
Drag options to blanks, or click blank then click option'
Aname
Bage
C'John'
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Not using the spread operator, which overwrites the whole object.
Mixing property names and values incorrectly.
5fill in blank
hard

Fill all three blanks to create a memoized callback that updates state only when dependencies change.

React
const increment = useCallback(() => {
  setCount([1] + 1);
}, [[2], [3]]);
Drag options to blanks, or click blank then click option'
Acount
CsetCount
Dincrement
Attempts:
3 left
💡 Hint
Common Mistakes
Leaving dependencies empty or missing required variables.
Using incorrect variable names in the dependencies.