React Routing Link Configuration Details

React Routing Link Configuration Details

1. Link's to attribute

(1) Place the routing path
(2) Place the object in the specified format
{pathname:"/xx",search:'?Key-value pair',hash:"#xxx",state:{key-value pair}}
It will automatically concatenate pathname, search, and hash to the url path. state is the passed parameter and you can view the information in the object by outputting props.
this.props.location.state.Key name to get the data in state

2. Link's replace attribute

Add replace to replace the previous page before the jump with the current page, and only push the current page into the stack

3. Link parameter transfer

Add "/key value" after the to path
Add "/:key name" after the route and path
In the component, the functional component: first pass in the props parameter, then props.match.params.key name class component: this.props.match.params.key name

Code example:

import React,{Component} from 'react'
//import {Route,BrowserRouter,Link} from 'react-router-dom'
//Rename BrowserRouter to Router
import { BrowserRouter as Router, Link, Route } from 'react-router-dom'
import { Button } from 'antd';
import './App.css';

function Home()
{
  return(
      <div>admin homepage</div>
    )
}

function Me(props)
{
  console.log(props)
  return(
      <div>admin my</div>
    )
}

function Product(props)
{
  return(
      <div>admin product page:{props.match.params.id}</div>
    )
}

export default class App extends Component {
   constructor()
    {
      super();
    }
    render()
    {
    {/*If the path is written in object form and is the same as below, pathname, search, and hash will be automatically concatenated on the url path, and state is the data passed into the component*/}
      let obj={pathname:"/me",search:'?username=admin',hash:"#abc",state:{msg:'hello'}}
      return(
        <div id='app'>
      {/*You can put multiple BrowserRouter*/}
          <Router>
        
        {/*Because the component also returns HTML content, you can directly return HTML content through the function to act as a component, but you cannot directly write HTML content*/}
          <div>  
            <Route path="/" exact component={()=><div>首页</div>}></Route>
            <Route path="/product" component={()=><div>product</div>}></Route>
            <Route path="/me" component={()=><div>me</div>}></Route>
          </div>
          {/*<Route path="/" component={function(){return <div>首页2</div>}}></Route>*/}

          </Router>
        

          
          {/*There can only be one root container inside BrowserRouter to wrap other content*/}
        {/*After adding basename='/xx', when you click Link to jump to other routes, the url will add /xx to the front of the route name, so both the route path and the route path with admin added can match the route*/}
          <Router basename='/admin'>
            <div>
                <div className='nav'>
                    <Link to='/'>Home</Link>
                    <Link to='/product/123'>Product</Link>
                  {/*You can output props in the corresponding component to view the information of the passed object, add replace to replace the previous page before the jump with the current page, and only push the current page into the stack*/}
                    <Link to={obj} replace>Personal Center</Link>
                </div>

                <Route path="/" exact component={Home}></Route>
                <Route path="/product/:id" component={Product}></Route>
                <Route path="/me" exact component={Me}></Route>
            </div>
          </Router>

        </div>
        
      )
    }
}

This is the end of this article about the detailed configuration of react routing Link. For more relevant react routing Link content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the redirection methods of each version of React routing
  • react-router JS control routing jump example
  • Detailed explanation of how react obtains routing parameters in components
  • How to use nested routes in react-router4
  • Implementation of React-router4 route monitoring
  • React router4+redux method to implement routing permission control
  • Summary of React-router v4 routing configuration method
  • Detailed explanation of routing applications above react router 4.0

<<:  Introduction to keyword design methods in web design

>>:  Summary of basic operations for MySQL beginners

Recommend

Detailed Example of CSS3 box-shadow Property

CSS3 -- Adding shadows (using box shadows) CSS3 -...

HTML table markup tutorial (9): cell spacing attribute CELLSPACING

A certain distance can be set between cells in a ...

ReactRouter implementation

ReactRouter implementation ReactRouter is the cor...

How to completely uninstall Docker Toolbox

Docker Toolbox is a solution for installing Docke...

Tutorial on how to quickly deploy clickhouse using docker-compose

ClickHouse is an open source column-oriented DBMS...

MySQL query sorting and paging related

Overview It is usually not what we want to presen...

Solve the cross-domain problem of Vue+SpringBoot+Shiro

Table of contents 1. Configure Vue front end 1. D...

Use Python to connect to MySQL database using the pymysql module

Install pymysql pip install pymysql 2|0Using pymy...

Comprehensive analysis of MySql master-slave replication mechanism

Table of contents Master-slave replication mechan...

TypeScript Enumeration Type

Table of contents 1. Overview 2. Digital Enumerat...

How to add ansible service in alpine image

Use apk add ansible to add the ansible service to...

We're driving IE6 to extinction on our own

In fact, we wonder every day when IE6 will really...

Solve the problem of running hello-world after docker installation

Installed Docker V1.13.1 on centos7.3 using yum B...