-
Notifications
You must be signed in to change notification settings - Fork 641
Infinite loop when dispatching redirect from within a componentWillReceiveProps #212
Comments
Why are you dispatching anything within a cWRP? That seems like a bad code pattern. |
Because |
It's kind of ugly, but you can include a simple circuit breaker in there. let checkingAuth = false;
//....
checkForAuth(props) {
if (checkingAuth) return;
checkingAuth = true;
const {dispatch, hasAuthError, location} = props;
let newKey = location && location.key || 'none';
const authToken = localStorage.getItem(socketOptions.authTokenName);
if (hasAuthError || !authToken && newKey !== key) {
key = newKey;
dispatch(push('/login?next=%2Fkanban'));
} else {
checkingAuth = false;
}
} |
Ah, that code already has a circuit breaker in place ( |
The loop is a common one: |
I definitely understand (I think it's the same bug that bit me in an |
I agree with @mattkrick that the 'community best practices' right now for auth + redux + react-router seems to be a HOC with dispatches in cWM and cWRP. Its a common answer on discord and there are several examples out there. I have a similar Authentication/Authorization wrapper in my own app using this approach, but have not had the infinite loop issues at all described here. I am, however, using I think it may be necessary to either come up with a best practice solution for this that is explicitly stated in the README and warns against potential places for error, or to actually add a rackt approved auth module to the ecosystem which everyone can use that handles these potential edge cases when using redux + routing. |
That wouldn't be a terrible thing to build. I know @ryanflorence and @mjackson want to keep growing Rackt, so if anyone in the community wants to step up and build something awesome for this, that would be a great entry point (webpack pun intended) into the org. |
Challenge accepted! I've got some scaffolding Im pretty happy with that should be pretty flexible for most use cases and tests/examples as well. I'll work on getting it pushed and hopefully get some feedback from @timdorr and others if its the right direction. Let me know if there are others I should ping once I feel like its in a good place. |
Shouldn't this still be open? The package is not compatible with standard react lifecycle actions and there is no warning saying so... |
It's not this package alone. It's a combination of redux and react-redux, as you can create the loop I talked about with any old action. I will add a warning about connecting location state, though. I can't think of a good reason to do that, and the router provides the same information to your route components already. |
Btw, onEnter does support async via an optional third param on your hook function. That's a done-style callback that you can use to resolve the async code. Just an FYI before I forget :) |
@timdorr sounds good! |
@timdorr @mattkrick I've created an auth wrapper library for redux here: https://github.com/mjrussell/redux-auth-wrapper Its designed more in mind to connect to a reducer that holds the store data (rather than using the local storage) but I could probably work on making it support that example. (it might already do except for being able to subscribe to a local storage change) It doesn't have any of the looping issues described in this ticket because it takes advantage of |
@mjrussell nice work, that was fast! Couple thoughts
|
@mattkrick Just to manage these issues better, let's move things over to issues on the repo itself. |
Was just going to say Im going to port those there. 😄 Thanks @mattkrick |
if I dispatch a
push
orreplace
from within acomponentWillReceiveProps
, it causes that component to receive new props, making for an infinite loop (at least I think that's the process, chrome profiler stops after the first loop, so I haven't traced it entirely). Would it be possible to do something like save thelocation.key
and if that key comes up twice in a row to just ignore the dispatch? Oddly enough, it doesn't happen if the redirect is to the/
...The text was updated successfully, but these errors were encountered: