question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onClick does not see state hooks changes

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
mklopetscommented, May 20, 2020

Should be fixed in the latest version, thanks to the PR by @sanfilippopablo. If true, let’s close this.

5reactions
qlerebourscommented, Dec 28, 2019

I also noticed that the behavior of onClick is captured and never changed. I will rewrite @crosskpixel example to be more comprehensible:

const ReactHookComponent = () => {
    const [count, setCount] = useState(0);

    useEffect(() => {
        setCount(c => c+1); // start by increment count from 0 to 1
    }, []);

    const handleClick = () => {
        console.log('Map clicked with count', count); // will display 'Map clicked with count 0'. State is not up to date
    }

   return (
        <Map onClick={handleClick} />
    )
}

At the moment, a crappy way to work this around is to use a ref:

    const countForMap = useRef(0);

    useEffect(() => {
       countForMap.current = count;
   }, [count])

   const handleClick = () => {
        console.log('Map clicked with count', countForMap.current); // will display 'Map clicked with count 1
    }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found