使用react-router4.0實現(xiàn)重定向和404功能的方法
在開發(fā)中,重定向和404這種需求非常常見,使用React-router4.0可以使用Redirect進行重定向
最常用的就是用戶登錄之后自動跳轉(zhuǎn)主頁。
import React, { Component } from 'react';
import axios from 'axios';
import { Redirect } from 'react-router-dom';
class Login extends Component{
constructor(){
super();
this.state = {value: '', logined: false};
this.handleChange = this.handleChange.bind(this);
this.toLogin = this.toLogin.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value})
}
toLogin(accesstoken) {
axios.post('yoururl',{accesstoken})
.then((res) => {
this.setState({logined: true});
})
}
render() {
if(this.props.logined) {
return (
<Redirect to="/user"/>
)
}
return (
<div>
<input type="text" value={this.state.value} onChange={this.handleChange} />
<a onClick={() => { this.toLogin(this.state.value) }}>登錄</a>
</div>
)
}
}
export default Login;
以上這個示例僅僅是將登錄的狀態(tài)作為組件的state使用和維護的,在實際開發(fā)中,是否登錄的狀態(tài)應(yīng)該是全局使用的,因此這時候可能你會需要考慮使用redux等這些數(shù)據(jù)狀態(tài)管理庫,方便我們做數(shù)據(jù)的管理。這里需要注意的使用傳統(tǒng)的登錄方式使用cookie存儲無序且復(fù)雜的sessionID之類的來儲存用戶的信息,使用token的話,返回的可能是用戶信息,此時可以考慮使用sessionStorage、localStorage來儲存用戶信息(包括頭像、用戶名等),此時書寫reducer時需要指定初始狀態(tài)從存儲中獲取,如:
const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
const LOGIN_FAILED = 'LOGIN_FAILED';
const LOGINOUT = 'LOGINOUT';
const Login = function(state, action) {
if(!state) {
console.log('state');
if(sessionStorage.getItem('usermsg')) {
return {
logined: true,
usermsg: JSON.parse(sessionStorage.getItem('usermsg'))
}
}
return {
logined: false,
usermsg: {}
}
}
switch(action.type) {
case LOGIN_SUCCESS:
return {logined: true,usermsg: action.usermsg};
case LOGIN_FAILED:
return {logined: false,usermsg:{}};
case LOGINOUT:
return {logined: false, usermsg: {}};
default:
return state
}
};
export default Login;
指定404頁面也非常簡單,只需要在路由系統(tǒng)最后使用Route指定404頁面的component即可
<Switch>
<Route path="/" exact component={Home}/>
<Route path="/user" component={User}/>
<Route component={NoMatch}/>
</Switch>
當路由指定的所有路徑?jīng)]有匹配時,就會加載這個NoMatch組件,也就是404頁面
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 詳解React中傳入組件的props改變時更新組件的幾種實現(xiàn)方法
- VSCode配置react開發(fā)環(huán)境的步驟
- react-redux中connect的裝飾器用法@connect詳解
- ReactNative 之FlatList使用及踩坑封裝總結(jié)
- React Native實現(xiàn)簡單的登錄功能(推薦)
- ReactNative之鍵盤Keyboard的彈出與消失示例
- react-router browserHistory刷新頁面404問題解決方法
- 詳解React Native開源時間日期選擇器組件(react-native-datetime)
- react中使用swiper的具體方法
- React useMemo和useCallback的使用場景
相關(guān)文章
React中使用Workbox進行預(yù)緩存的實現(xiàn)代碼
Workbox是Google Chrome團隊推出的一套 PWA 的解決方案,這套解決方案當中包含了核心庫和構(gòu)建工具,因此我們可以利用Workbox實現(xiàn)Service Worker的快速開發(fā),本文小編給大家介紹了React中使用Workbox進行預(yù)緩存的實現(xiàn),需要的朋友可以參考下2023-11-11
React videojs 實現(xiàn)自定義組件(視頻畫質(zhì)/清晰度切換) 的操作代碼
最近使用videojs作為視頻處理第三方庫,用來對接m3u8視頻類型,這里總結(jié)一下自定義組件遇到的問題及實現(xiàn),感興趣的朋友跟隨小編一起看看吧2023-08-08
React從react-router路由上做登陸驗證控制的方法
本篇文章主要介紹了React從react-router路由上做登陸驗證控制的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
可定制React自動完成搜索組件Turnstone實現(xiàn)示例
這篇文章主要為大家介紹了可定制React自動完成搜索組件Turnstone實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

