React Router 5.1.0 uses useHistory to implement page jump navigation

React Router 5.1.0 uses useHistory to implement page jump navigation

In React Router v4 you can use

  1. withRouter Component
  2. Use Tags

1. Use the withRouter component

The withRouter component will inject the history object as a property of the component

import React from 'react'
import { withRouter } from 'react-router-dom'
import { Button } from 'antd'

export const ButtonWithRouter = withRouter(({ history }) => {
  console.log('history', history)
  return (
    <Button
      type='default'
      onClick={() => { history.push('/new-location') }}
    >
      Click Me!
    </Button>

  )
})

image.jpg

import { ButtonWithRouter } from './buttonWithRouter'

or:

const ButtonWithRouter = (props) => {
  console.log('props', props)
  return (
    <Button
      type='default'
      onClick={() => { props.history.location.push('/new-location') }}
    >
      Click Me!
    </Button>

  )
}

export default withRouter(ButtonWithRouter)

image.jpg

Import: import ButtonWithRouter from './buttonWithRouter'

2. Use Route Tags

At the route entrance

image.jpg

The Route component is not only used for matching locations. You can render a route without a path and it will always match the current location. The Route component passes the same properties as withRouter, so the history methods can be accessed through the history properties.

so:

export const ButtonWithRouter = () => (
  <Route render={({ history }) => {
    console.log('history', history)
    return (
      <button
        type='button'
        onClick={() => { history.push('/new-location') }}
      >
        Click Me!
      </button>
    )
  }} />
) 

image.jpg

React Router 5.1.0 uses useHistory

Starting from React Router v5.1.0, the useHistory hook has been added. If you are using React >16.8.0, you can use useHistory to achieve page jump

export const ButtonWithRouter = () => {
  const history = useHistory();
  console.log('history', history)
  return (
    <button
      type='button'
      onClick={() => { history.push('/new-location') }}
    >
      Click Me!
    </button>
  )
}

image.jpg

This is the end of this article about the implementation of page jump navigation using useHistory in React Router 5.1.0. For more relevant ReactRouter useHistory page jump navigation content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • An example of implementing a breadcrumb navigation using React high-order components
  • React Navigation for Android Development Navigation Bar Style Adjustment + Bottom Corner Message Prompt
  • React Native react-navigation navigation usage details
  • Detailed explanation of cross-tab routing processing in React Native using react-navigation
  • Detailed explanation of React Native top and bottom navigation usage tips
  • React-native example of using react-navigation for page jump navigation
  • React realizes secondary linkage of navigation bar

<<:  Flash embedded in HTML Solution for embedding Flash files in HTML web page code (Part 1)

>>:  MySQL uses events to complete scheduled tasks

Recommend

How to restore a single database or table in MySQL and possible pitfalls

Preface: The most commonly used MySQL logical bac...

Several ways to implement 0ms delay timer in js

Table of contents queueMicrotask async/await Mess...

MySQL query sorting and paging related

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

Three networking methods and principles of VMware virtual machines (summary)

1. Brigde——Bridge: VMnet0 is used by default 1. P...

Vue implements the digital thousands separator format globally

This article example shares the specific code for...

How to use dynamic parameters and calculated properties in Vue

1. Dynamic parameters Starting from 2.6.0, you ca...

Solution to the inaccessibility of Tencent Cloud Server Tomcat port

I recently configured a server using Tencent Clou...

How to modify Ubuntu's source list (source list) detailed explanation

Introduction The default source of Ubuntu is not ...

MySQL Interview Questions: How to Set Up Hash Indexes

In addition to B-Tree indexes, MySQL also provide...

Ubuntu 20.04 CUDA & cuDNN Installation Method (Graphical Tutorial)

CUDA installation download cuda Enter the nvidia-...

HTML page common style (recommended)

As shown below: XML/HTML CodeCopy content to clip...

mysql5.7.21.zip installation tutorial

The detailed installation process of mysql5.7.21 ...