salvoravida / redux-first-history

Redux history binding support react-router - @reach/router - wouter - react-location
https://wvst19.csb.app/
MIT License
446 stars 34 forks source link

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

Closed matinrco closed 2 years ago

matinrco commented 2 years ago

hey! thanks for your great work.

here is sandbox

salvoravida commented 2 years ago

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 commented 2 years ago

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:

so basically it's your second workaround 👍

salvoravida commented 2 years ago

@matinrco fixed here https://github.com/salvoravida/redux-first-history/issues/88