Skip to content
Advertisement

React useState, setState and {state} in return

I come across the rendering issue with React State.
The problem is that {state} in return get value one beat late.
But the console log in handleChange shows right value.
If the previous value of state is 9, current value of state value is 10 then the console.log({state}) in handleChange shows 10 and the <span>{state}<span> in return shows 9.
It looks different from other state async problem.
I can’t understand why this happened.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

Two problems…

  1. findText will not have been updated to the new value when you use it in split(). Either use str instead or calculate findCount in a memo or effect hook with a dependency on findText.
  2. You’re completely misusing filter(). Use reduce() to calculate a computed sum
JavaScript

Edit autumn-river-jslqtt

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement