React Router 如何使用history跳轉(zhuǎn)的實現(xiàn)
在react-router中組件里面的跳轉(zhuǎn)可以用<Link>
但是在組件外面改如何跳轉(zhuǎn),需要用到react路由的history
replace方法和push方法使用形式一樣,replace的作用是取代當(dāng)前歷史記錄
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;}
}
如果不調(diào)用history.go則路由改變了,但是頁面不會跳轉(zhuǎn)。
到此這篇關(guān)于React Router 如何使用history跳轉(zhuǎn)的實現(xiàn)的文章就介紹到這了,更多相關(guān)React Router history跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
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)境的方法,在這篇文章中我們開始利用我們之前所學(xué)搭建一個簡易的React開發(fā)環(huán)境,用以鞏固我們之前學(xué)習(xí)的Webpack知識。一起跟隨小編過來看看吧2018-05-05
react 通過后端接口實現(xiàn)路由授權(quán)的示例代碼
本文主要介紹了React應(yīng)用中通過后端接口獲取路由授權(quán),實現(xiàn)動態(tài)和靈活的權(quán)限管理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11

