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

useTrackEvents skipFirstRun not working properly in React strict mode #54

Open
szilagyi-sandor opened this issue Aug 14, 2023 · 2 comments

Comments

@szilagyi-sandor
Copy link
Contributor

When you run your application in development mode and with the strict mode on the skipFirstRun parameter is not working properly. The reason for this is that when the strict mode tries to identifying unsafe lifecycles it will rerender your component making it call the function twice by default. This is not going to happen in production environment, but can be confusing in dev mode.

Steps to Reproduce

  • OS/Browser: Windows/Chrome
  • React Version: 18.2.0

Use the useTrackEvent hook in dev mode with the strict mode on.

Expected behavior
useTrackEvents skipFirstRun parameter works properly in development with strict mode.

Additional context
To prevent this issue, all you need to do is set the firstRun variable back to true, when the hook unmounts. I can create a PR for this if you want.

Thanks!

@Karlie-777
Copy link
Contributor

Karlie-777 commented Aug 14, 2023

Thanks @szilagyi-sandor you are right, since React 18 won't silence the logs as React 17 does. Our current version should be compatible with React 18 but React 17 is still the major supported version, see Compatibility Matrix . Maybe we should add something like return () => { firstRun = true } for useEffect and possibly more updates for React 18 changes. Please feel free to send out a PR (but we are not sure of verion18 release date yet)

@szilagyi-sandor
Copy link
Contributor Author

The only addition to the change is that instead of setting it to true, it needs to be set to the default skipFirstRun prop. I created a PR for it :)

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

No branches or pull requests

3 participants