2

Problem:

Have async route that get param movie from it and return Movie Container (component). The problem is when I go from one MovieContainer to another MovieContainer (just same route, but with diff. params) my router unmount and re-render MovieContainer component. But it should just pass new props and don't touch my component lifesycle. How to solve it ? What I do wrong ?

Code:

Router

<Route path="/" component={AppLayout}>
 <IndexRoute components={{ children: Home }} />
 <Route path=":movie/about" getComponents={(nextState, callback) => {
 window.scrollTo(0, 0);
 const movie = findMovie(nextState.params.movie);
 return callback(null, { children: (router) => <Movie movie={movie} router={router} /> });
 }}/>
 ....
</Route>

Component WillMount in Movie component

 componentWillMount() {
 console.log('MOUNT MovieContainer');
 }

UPDATE: OK, Done some experiment.

Replace

return callback(null, { children: (router) => <Movie movie={movie} router={router} /> });

With

return callback(null, Movie);

Andd Movie component not re-renderd. Just updated. It's great! But how to pass props now ?

asked Jan 3, 2017 at 13:27
4
  • This is correct and desired behavior. New routes should re-mount all components. Commented Jan 3, 2017 at 14:35
  • @AndyRay but it's not new route, just /movie1/about to /movie2/about This route pattern /:someparams created only for passing new props to the same component. So, why it should re-render ? Commented Jan 3, 2017 at 14:36
  • That is absolutely a new route. Commented Jan 3, 2017 at 14:37
  • @AndyRay ok, for example I have an app where I watch movies and have side bar that changes when my router changes like /movie/menu /movie/actors. And you try to tell me that it's OK that my movie will be re-renered every time when I just change my sidebar ? Commented Jan 3, 2017 at 14:40

1 Answer 1

1

Great! Finally I found solution.

<Route path=":movie/about" getComponents={(nextState, callback) => {
 window.scrollTo(0, 0);
 callback(null, Movie);
}}
/>

And in component I can access props via this.props.params.movie than find movie in constructor/componentWillMount const movie = findMovie(nextState.params.movie);

answered Jan 3, 2017 at 15:16
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.