nytimes / react-tracking

🎯 Declarative tracking for React apps.
https://open.nytimes.com/introducing-react-tracking-declarative-tracking-for-react-apps-2c76706bb79a
Other
1.87k stars 123 forks source link

Question: How can I access the the current value of variables outside of dispatch after e.g. state changes #225

Open natterstefan opened 10 months ago

natterstefan commented 10 months ago

Hello everyone,

I am dealing with the following problem:

Here is an example: https://codesandbox.io/s/react-tracking-example-mdqh9h?file=/src/App.js. Could you please look at line 28+ to see what I mean, and open the console to see the logs?

tizmagik commented 9 months ago

Thanks for submitting an issue @natterstefan -- I think this is because the current useTracking() hook does not accept a dependency array (so it never sees the updated state). I guess technically it could, but that has other implications and might be a bigger change than it's worth.

As a workaround, using a useRef for the dispatchInfo, seems to do the trick? I updated your codesandbox example here.

Let me know if this works for you!