MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Routing


Place a file like the following in your top level directory. It defines which components to render for which paths

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import New from './containers/new-post';
import Show from './containers/show';
import Index from './containers/home';
import App from './components/app';
export default(
 <Route path="/" component={App}>
 <IndexRoute component={Index} />
 <Route path="posts/new" component={New} />
 <Route path="posts/:id" component={Show} />
 </Route>
);

Now in your top level index.js that is your entry point to the app, you need only render this Router component like so:

i
mport React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
// import the routes component we created in routes.js
import routes from './routes';
// entry point
ReactDOM.render(
 <Router history={browserHistory} routes={routes} />
 , document.getElementById('main'));

Now it is simply a matter of using Link instead of tags throughout your application. Using Link will communicate with React Router to change the React Router route to the specified link, which will in turn render the correct component as defined in routes.js

import React from 'react';
import { Link } from 'react-router';
export default function PostButton(props) {
 return (
 <Link to={`posts/${props.postId}`}>
 <div className="post-button" >
 {props.title}
 <span>{props.tags}</span>
 </div>
 </Link>
 );
}


Conclusion

In this page (written and validated by ) you learned about React Routing . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React Routing Async.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.