onClick does not see state hooks changes
See original GitHub issueI am using react 16.8 hooks, in the map component I define an onClick function, but when I retrieve the value of the state hooks me the initial value is returned.
in the visualization it is possible to see the updated value of count, but within the function it is not possible
const [count,setCount] = useState(0); ... .... ... onClick={(map, evt) => { console.log(count); // Caso o indicador criando marcador esteja ativo ira criar marcadores no mapa. const { lng, lat } = evt.lngLat; setCriandoMarcador(true); if (criandoMarcador) { setPontosGps([...pontosGps, { lng, lat }]); if (!travarFerramenta) { setCriandoMarcador(false); } } }}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:15 (1 by maintainers)
Top Results From Across the Web
React hook useState not updating onclick - Stack Overflow
If you will check the state update immediately after updating the state react then you may not get the updated state. You can...
Read more >onClick does not see state hooks changes #963 - GitHub
The issue of #748 is popping up again in v5.0.0 and newer ( v5.1.1 ). It was fixed in v4.8.6 . In short:...
Read more >Developers - onClick does not see state hooks changes -
In short: when using React hooks, the onClick listener is bound once at initialization it seems, and is not replaced with newer instances...
Read more >Handling Events - React
Handling events with React elements is very similar to handling events on DOM elements. There are some syntax differences: React events are named...
Read more >useOnClickOutside React Hook - useHooks
Hooks are a feature in React that allow you use state and other React features ... This hook allows you to detect clicks...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Should be fixed in the latest version, thanks to the PR by @sanfilippopablo. If true, let’s close this.
I also noticed that the behavior of onClick is captured and never changed. I will rewrite @crosskpixel example to be more comprehensible:
At the moment, a crappy way to work this around is to use a ref: