Notes in Migrating From React-Router-3 to React-Router-4
Created|Updated
|Post View:|Comments:
HashRouter
Version 4 of React-Router seperate top level router element for the different history type. If you’re using version 4 you should be able to replace <Router hisotry={hashHistory}> with <HashRouter>
// Each logical 'route' has two components, one for the sidebar and one for the main area. We want to render both of them in different places when the path matches the current URL.
{routes.map((route, index) => ( // You can render a <Router> in as many places as you want in your app, it will render along with any other <Router>s that also match the URL. // So a sidebar or breadcrumbs or anything else that requires you to render multiple things in multiple places at the same URL is nothing more than multiple <Routes>s
// main <div style={{ flex: 1, padding: '30px' }}> {routes.map((route, index) => ( // Render more <Route>s with the same paths as above, but different components this time. <Route key={index} path={route.path} exact={route.exact} component={route.main} /> ))} </div>
<div style={style.content}> <ReactCSSTransitionGroup transitionName='fade' transitionEnterTimeout={300} transitionLeaveTimeout={300} > // no different than other usage of ReactCSSTransitionGroup, just make sure to pass `location` to `Route` so it can match the old location as it animate out <Route location={loaction} key={location.key} path="/:h/:s/:l" component={HSL} /> </ReactCSSTransitionGroup> </div> </div> )}/> </Router> )
// Some folks find value in a centralized route config. A route config is just data. React is great at mapping data into component, and <Route> is a component.
Generally, React Router and Redux work just fine together. Occasionally though, an app have a component that doesn’t update when the location changes (child route or active nav links don’t update)
This happens if:
The component is connected to redux via connect()(Comp) The component is not a “route component”, meaning it is not rendered like so: <Route component={SomeConnectedThing}/>
The problem is that Redux implements shouldComponentUpdate and there’s no indication that anything has changed if it isn’t receiving props from the router. This is straightforward to fix. Find where you connect your component and wrap it in withRouter
One great feature of the web is that we don’t have to make our visitors download the entire app before they can use it. You can think of code spliting as incrementally downloading the app. While there are other tools for the job, react-router use webpack and the bundle loader.