Router Link React router
Made out of frustration with react-router
, this router library is incredibly easy to use.
INSTALLATION
npm i router-link
or yarn add router-link
USAGE
An example application can be found here: React App Boilerplate with Mobx
// Define your routes in a file called routes.js const routes = path: '/' view: Home path: '/about' view: About path: '/login' view: Login path: '/chat' view: Chat path: '*' view: NotFound // In your App.js, the top level component { return <div className="App"> <header className="App-header"> <Navigation/> </header> <main className="App-main"> <Router routes= routes /> </main> </div> } // Use the Link to create links for navigation// They will create a-tags with an active class for styling. { return <Link to="/login">Log in</Link> to get started! }
NAVIGATION
In your components you can use the router.push('/path')
to programmatically trigger a route:
// Import the router library // Do this anywhere inside your component to redirectrouter
DYNAMIC ROUTES
Define dynamic routes by adding a :
in front of the sub-path, and get the parameters as props:
// Define route with parametersconst routes = path: '/messages/:id' view: Messages // Use like this<Link to= `/messages/` >Message</Link>
In this case the the id
will be available as this.props.route.id
in your component.
LICENSE
MIT Licensed. Enjoy!