In React Router v4 you can use
1. Use the withRouter componentThe 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> ) }) 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) Import: 2. Use Route Tags At the route entrance 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> ) }} /> ) React Router 5.1.0 uses useHistoryStarting 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> ) } 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:
|
<<: Flash embedded in HTML Solution for embedding Flash files in HTML web page code (Part 1)
>>: MySQL uses events to complete scheduled tasks
Table of contents MySQL result sorting - Aggregat...
CSS3 can change the color of pictures. From now o...
Problem: The partition where MySQL stores data fi...
This article shares the specific code of the canv...
Docker Overview Docker is an open source software...
{ {}} Get the value, the original content of the ...
How to install Nginx in a specified location in C...
1: Differences in speed and loading methods The di...
Sometimes some docker containers exit after a per...
Table of contents Preface Quick Review: JavaScrip...
Before using idea to write JSP files, you need to...
The installation tutorial of mysql5.7.17 is share...
The installation tutorial of mysql 8.0.11 winx64 ...
Introduction to Load Balancing Before introducing...
Table of contents Time zone configuration in Djan...