Skip to content

Doesn't work with { connect } from 'redux'. #4252

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

Closed
FateRiddle opened this issue Dec 7, 2016 · 5 comments
Closed

Doesn't work with { connect } from 'redux'. #4252

FateRiddle opened this issue Dec 7, 2016 · 5 comments

Comments

@FateRiddle
Copy link

FateRiddle commented Dec 7, 2016

basically, write a component that changes when url changes (wraps inside <BrowserRouter>):
let Path = () => <div> {location.pathname}</div>
then import { connect } from redux and
Path = connect()(Path)

Then <Path /> will no longer update itself when you change url. Why and how to solve it?

I don't really need to have everything stored inside redux store like suggested in react-router-addons-controlled. Just want redux to work with components that has its own state.

@pshrmn
Copy link
Contributor

pshrmn commented Dec 7, 2016

The connect HOC's shouldComponentUpdate is returning false, which prevents your <Path> component from updating. You will either need to make the connect impure or add a subscriber. For an example of the subscriber pattern, check out the <Link> component.

@FateRiddle
Copy link
Author

So simply put, I can't use connect from redux if I decide to go with react-router v4?

@pshrmn
Copy link
Contributor

pshrmn commented Dec 7, 2016

No, you can use redux, but you need to be aware that react-redux uses shouldComponentUpdate as a performance enhancement. shouldComponentUpdate does not take into account context changes, so it returns false on re-renders triggered by location changes. You will need to take measures to work around this in order to ensure that connected components (and their children) are updated when the location changes. Either of the options that I mentioned will work.

@FateRiddle
Copy link
Author

FateRiddle commented Dec 7, 2016

So I have to write every component in class form (instead of function short form), then subscribe to location inside componentDidMount()? How do I write the code? Would you mind giving more details?I'm still in learning process. Wait... router has a subscribe method?

@timdorr
Copy link
Member

timdorr commented Dec 7, 2016

Just so you know, we are looking to remove sCU usage in an upcoming version of react-redux: reduxjs/react-redux#507

But this is a dupe of #4035

@timdorr timdorr closed this as completed Dec 7, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants