简体   繁体   中英

What is the correct approach when combining React, Redux and React-Router?

My goal is to create SPA based on React using Redux and React-Router.

How should I store my location and history in order to create dynamic Link components?

eg:

<Link to={lastVisitedPage}> </Link>

there are two approaches for this in your case;

First:

 <Route path="/" onChange={yourHandler} component={AppContainer}>
   <IndexRoute component={StaticContainer}  />
   <Route path="/a" component={ContainerA}  />
   <Route path="/b" component={ContainerB}  />
 </Route>

function yourHandler(previousRoute, nextRoute) {
   //do your logic here
}

then you can have your redux logic in yourHandler() function. In the second way of doing this is to:

import { browserHistory } from 'react-router';

//Your initialization

browserHistory.listen( location =>  {
 //Do your stuff here
});

have this in your app.js and have your redux logic in the listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM