React Router in 3 minutes

React Router in 3 minutes

Implement routing into a React app is so easy, the first step is to install react-router -dom package.

npm i react-router-dom

In the App.js import 3 essentials elements BrowserRouter, Route and Switch from react-router-dom, these handle our routes, create links and load components.

import { BrowserRouter as Router, Route, Switch } from “react-router-dom”;

Every one of these components has a function in the process of add routing in our app.

The BrowserRouter is the parent component that stores all our routes and it will wrap our app in App.js.

<Router>  
<div className="App container">  
<Header />  
 <My-component>  
</Router>

The Switch allows set the area to be replaced with the component when it matches with the route path.

The Route accepts the exact parameter and the path it will set the path like /about or /add and set witch component will be load.

<Router>  
<div className="App container">  
<Header/>  
<Switch>  
 <Route exact path="/" component={ContactsList} />  
 <Route exact path="/add" component={Signup} />  
 <Route exact path="/about" component={About} />   
</Switch>  
</Router>

HOW TO SET NOTFOUND PAGE?

Set a single route without path it will be load by default if some path doesn’t exist like https://localhost/lebroninplayoffs.

<Route component={NotFound} />

HOW TO LINK OUR ROUTES

Import and use the Link component it allows load our routes it contains a property to with your path.

import { Link } from 'react-router-dom'

<Link to="/about"> About</link>

HOW TO PASSING AND READ PARAMETERS

To configure our route to accept params, in the route component set the path with: and the parameter name.

<Route exact path="/about/:id" component={About} />

The params can be read throw props.

<p>{props.match.params.id}</p>

HOW TO REDIRECT TO ROUTE WITHOUT LINK?

Using this.props.history.push and passing the path you can move throw routes.

this.props.history.push(‘/about’)

Happy Routing.



要查看或添加评论,请登录

Dany Paredes的更多文章

  • November 2024 - DevFests and Community

    November 2024 - DevFests and Community

    Hi! ???? November has ended, and it was packed with four events in a single month ?? (special thanks to my family…

    3 条评论
  • Testing javascript from 0 to Mocha.

    Testing javascript from 0 to Mocha.

    I’m trying to learn #testing in javascript, today we have few ways to test our code write plain test code or using…

  • Como crear typings para librerías en Typescript ?

    Como crear typings para librerías en Typescript ?

    Si algo tengo que reconocer es que Typescript nos permite sentir que tenemos el control en javascript y eso es gracias…

  • Hola Vue-Cli

    Hola Vue-Cli

    Cuando queremos desarrollar una aplicación con Vue con toda la potencia de ES6, ademas detener un entorno de desarrollo…

  • Directivas de VueJS!

    Directivas de VueJS!

    Vue nos ofrece un listado de directivas para facilitar el desarrollo de SPA o componentes, las cuales nos permiten…

  • Conociendo Vuejs

    Conociendo Vuejs

    Hace unos meses le comente a un amigo @oriolpuigbuil sobre Vuejs, pero no le di continuidad, pensé que seria un…

    6 条评论
  • ES6!

    ES6!

    Si vas a hacer un proyecto nuevo o simplemente vas a una entrevista como Javascript developer, hay puntos ES6 que…

  • Multi-idioma con Mustache y i18njs

    Multi-idioma con Mustache y i18njs

    Cuando hablamos de web multi-idioma en asp.net es un tema resuelto gracias a los resources, pero para una SPA en…

  • SPA en 2016 ?

    SPA en 2016 ?

    Primeramente, recomiendo como lectura obligatoria la presentación de @CKgraficosobre la evolución de desarrollo…

  • Crea y Organiza tus Tests con Jasmine

    Crea y Organiza tus Tests con Jasmine

    Uno de los puntos quizás más complejos en Jasmine ,es la forma de organizar los tests, Jasmine nos permite mucha…

    2 条评论

社区洞察

其他会员也浏览了