Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
natterstefan opened this issue Sep 19, 2023 · 1 comment
Labels

Comments

@natterstefan
Copy link

natterstefan commented Sep 19, 2023

Hello everyone,

I am dealing with the following problem:

  • I have a component that sets up the app-wide context (useTracking({ context })
  • context is updated async after the component mounts and sometimes even when the route changes
  • additional info is relevant to dispatch but should not be part of the event (not added to context), let's say it is called dispatchInfo
  • the logic inside dispatch should be able to access the current value of context and dispatchInfo at any time, but it doesn't. It's stuck to the first value it gets.

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
Copy link
Collaborator

tizmagik commented Oct 2, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants