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

Using store location object causes infinite renders but it's fine with rr6 location hook #80

Closed
matinrco opened this issue May 6, 2022 · 3 comments

Comments

@matinrco
Copy link

matinrco commented May 6, 2022

hey! thanks for your great work.

here is sandbox

@matinrco matinrco changed the title Using store location object causes infinite renders on initial load but it's fine with rr6 location hook Using store location object causes infinite renders but it's fine with rr6 location hook May 6, 2022
@salvoravida
Copy link
Owner

Hi!
the problem is that Navigate has IMHO a bug here https://github.com/remix-run/react-router/blob/main/packages/react-router/lib/components.tsx#L95. not having [] for running only once.

dispatch a new location make re-render components subscribed to, and the loop is generated because Navigate runs effects every render.

So you have 2 options

  1. read the location from redux and use a NavigateFixed component
    https://codesandbox.io/s/redux-first-history-test-forked-pguxh1?file=/src/PrivateRoute.tsx

  2. read location from useLocation (that has a copy stored on context) and is not subscribed to redux.

@matinrco
Copy link
Author

matinrco commented May 6, 2022

So you have 2 options

  1. read the location from redux and use a NavigateFixed component
    https://codesandbox.io/s/redux-first-history-test-forked-pguxh1?file=/src/PrivateRoute.tsx

  2. read location from useLocation (that has a copy stored on context) and is not subscribed to redux.

I fixed that by applying a convention for myself:

  • use rr stuff in components ( navigation components, hooks, etc)(obviously)
  • use store router data with combination of redux-first-history actions for any out of component navigation (in slice/thunks ..)
    and don't mix this usage xd

so basically it's your second workaround 👍

@salvoravida
Copy link
Owner

@matinrco fixed here #88

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

No branches or pull requests

2 participants