Skip to content
Justice Tech School

Functional Updates

When the next value depends on the previous one, pass a function.

Example
const [n, setN] = useState(0);
function addThree() {
  setN((c) => c + 1);
  setN((c) => c + 1);
  setN((c) => c + 1);
}

Exercise

Previous value.

setN((c)  c + 1)

Quiz Test

Why use setN(c => c + 1)?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.