Skip to content

fix: ssr issue, move fns inside useEffect #2630

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

awareness481
Copy link

@awareness481 awareness481 commented Apr 12, 2025

Description

The hook as is is not SSR safe (repro: https://ck29z6-3000.csb.app/ ). The timestamp is initialized on the server and on the client as Date.now() so it's guaranteed to cause a hydration issue when .timestamp is used on the DOM (unless the client's clock is behind and by chance it happens to be the same during client rendering).

Additionally, there's no point in initializing .timestamp as it arguably should only be initialized/set by the geolocation events.

Additional changes include: moving the functions inside useEffect so they're not needed in the deps array, removing mounted as it's a useless check.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • Read the Contributing Guide
  • Perform a code self-review
  • Comment the code, particularly in hard-to-understand areas
  • Add documentation
  • Add hook's story at Storybook
  • Cover changes with tests
  • Ensure the test suite passes (yarn test)
  • Provide 100% tests coverage
  • Make sure code lints (yarn lint). Fix it with yarn lint:fix in case of failure.
  • Make sure types are fine (yarn lint:types).


return () => {
mounted = false;
navigator.geolocation.clearWatch(watchId);
};
}, []);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, options should be added in the deps array but the changes the behavior of the hook

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

Successfully merging this pull request may close these issues.

1 participant