React Router 如何使用history跳轉的實現(xiàn)
在react-router中組件里面的跳轉可以用<Link>
但是在組件外面改如何跳轉,需要用到react路由的history
replace方法和push方法使用形式一樣,replace的作用是取代當前歷史記錄
go,此方法用來前進或者倒退,history.go(-1);
goBack,此方法用來回退,history.goBack();
goForward,此方法用來前進,history.goForward();
1.hook
import {useHistory} from 'react-router-dom';
function goPage(e) {
history.push({
pathname: "/home",
state: {id: 1}
});
}
pathname是路由地址,state可以傳參
獲取參數(shù):
import {useLocation} from 'react-router-dom';
function getParams(){
let location = useLocation();
let id = location.state.id;
}
2.class組件
import React from 'react';
import {createBrowserHistory} from "history";
class App extends React.Component{
constructor(props) {
super(props);
}
goPage() {
let history = createBrowserHistory()
history.push({
pathname: "/home",
state: {id: 1}
});
history.go();
}
render() {return null;}
}
如果不調用history.go則路由改變了,但是頁面不會跳轉。
到此這篇關于React Router 如何使用history跳轉的實現(xiàn)的文章就介紹到這了,更多相關React Router history跳轉內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
React-router4路由監(jiān)聽的實現(xiàn)
這篇文章主要介紹了React-router4路由監(jiān)聽的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
使用webpack搭建react開發(fā)環(huán)境的方法
本篇文章主要介紹了使用webpack搭建react開發(fā)環(huán)境的方法,在這篇文章中我們開始利用我們之前所學搭建一個簡易的React開發(fā)環(huán)境,用以鞏固我們之前學習的Webpack知識。一起跟隨小編過來看看吧2018-05-05

