Skip to content

React not rendering view when Link tag used inside redux #4756

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
themirror178 opened this issue Mar 15, 2017 · 6 comments
Closed

React not rendering view when Link tag used inside redux #4756

themirror178 opened this issue Mar 15, 2017 · 6 comments

Comments

@themirror178
Copy link

After doing some researching, I found a similar issue regarding Redux's connect and {pure: false} workaround was fixed in react-router 3.0. I'm running react-router 4.0 (as part of react-router-dom) and still find this issue. reduxjs/react-redux#388

//bootstrap

ReactDOM.render(
    <Provider store={myStore}>
        <Router>
            <ConnectedMain />
        </Router>
    </Provider>, root_elem);

var ConnectedAdminMain = connect( mapStateToProps, null, null)(Main);

// Main

<div className="col-lg-12">
   <Route exact path="/" component={TestOne} />
      <Route path="/:id" component={ TestOneDetails} />
   </div>

// TestOne
Has <Link to="/foo">bar</Link>

Version

4.0.0
"react-redux": "^5.0.2",
"react-router-dom": "^4.0.0",

Test Case

Steps to reproduce

Expected Behavior

  1. Link changes in URL
  2. View re-renders

Actual Behavior

  1. Link changes in URL
  2. View does not re-render
  3. Refreshing the browser renders the correct view.
@pshrmn
Copy link
Contributor

pshrmn commented Mar 15, 2017

{ pure: false } would fix your issue. Otherwise, please see https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/blocked-updates.md (or if you want a longer read, you can check out this article I wrote that goes into some more detail about the problem).

@pshrmn pshrmn closed this as completed Mar 15, 2017
@themirror178
Copy link
Author

Is { pure: false } the solution, or the workaround? Apart from using HOCs or pathless routes, from your post.

@pshrmn
Copy link
Contributor

pshrmn commented Mar 16, 2017

Anything that allows for your connected component to update when the location changes would work. Passing a location prop means that sCU can still block updates when the location has not changed. Whether this is important will vary from project to project, but I would say don't feel obligated to turn off all update blocking just for React Router's sake.

@vovacodes
Copy link

vovacodes commented Mar 18, 2017

What about the nested <Route> components? Are they prone to the same issue?

React.render(
  <Router>
      <Route path="/", render={({ match }) => <App matchUrl={match.url}>} />
  </Router>
)

class App extends PureComponent  {
  render() {
    const { matchUrl } = this.props;
    return (
       <div>
           <Link to={matchUrl} >Home</Link>
           <Link to={`${matchUrl}about`} >Home</Link>
           <Route exact path={matchUrl} render={() => <h1>Home Component</h1>} />
           <Route path={`${matchUrl}about`} render={({ location }) => <h1>About {location.url}</h1>} />
       </div>
    )
  }
}  

In other words, does it mean I always have to pierce all the shouldComponentUpdates between Routes as well?

@edwardfxiao
Copy link

edwardfxiao commented Jul 13, 2017

import { withRouter } from 'react-router-dom'
export default withRouter(connect(mapStateToProps)(Something))

this does the trick. https://reacttraining.com/react-router/web/guides/redux-integration

@remix-run remix-run deleted a comment from lazdinst Dec 7, 2017
@hlgrondijs
Copy link

hlgrondijs commented Dec 10, 2018

withRouter() works, but you have to put it on the right component. For me that was the child of the <Router> component.
Too bad none of the solutions mentioned on which component you had to put the "withRouter()" nonsense

@lock lock bot locked as resolved and limited conversation to collaborators Feb 8, 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

5 participants