Accepted answer

There are two issues here:

  • Calling a state setter won't reassign the stateful const variable in the current render - you need to wait until the next render for count to be updated, so

    const handleChange = () => {
        setCount(count + 1);
        document.title = `You clicked ${count} times`;

    won't work. Your second snippet will result in You clicked 0 times after a single click.

  • Even if that were possible, when there are multiple places where a state setter may be called, putting something that should happen afterwards after every single one isn't very maintainable - it'd be better to be able to put that code to run afterwards in only one place.

Related Query

More Query from same tag