欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React從react-router路由上做登陸驗證控制的方法

 更新時間:2018年05月10日 16:32:18   作者:戀月  
本篇文章主要介紹了React從react-router路由上做登陸驗證控制的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了React從react-router路由上做登陸驗證控制的方法,分享給大家,具體如下:

驗證代碼

import React from 'react'
import {connect} from 'react-redux';

function requireAuthentication(Component) {
 // 組件有已登陸的模塊 直接返回 (防止從新渲染)
 if (Component.AuthenticatedComponent) {
  return Component.AuthenticatedComponent
 }

 // 創(chuàng)建驗證組件
 class AuthenticatedComponent extends React.Component {
  static contextTypes = {
   router: React.PropTypes.object.isRequired,
  }

  state = {
   login: true,
  }

  componentWillMount() {
   this.checkAuth();
  }

  componentWillReceiveProps(nextProps) {
   this.checkAuth();
  }

  checkAuth() {

   // 判斷登陸
   const token = this.props.token;
   const login = token ? token.login : null;


   // 未登陸重定向到登陸頁面
   if (!login) {
    let redirect = this.props.location.pathname + this.props.location.search;
    this.context.router.push('/login?message=401&redirect_uri=' + encodeURIComponent(redirect));
    return;
   }

   this.setState({login});
  }

  render() {
   if (this.state.login) {
    return <Component {...this.props}/>
   }
   return ''
  }
 }

 // 不使用 react-redux 的話直接返回
 // Component.AuthenticatedComponent = AuthenticatedComponent
 // return Component.AuthenticatedComponent


 function mapStateToProps(state) {
  return {
   token: state.token,
  };
 }

 function mapDispatchToProps(dispatch) {
  return {};
 }
 Component.AuthenticatedComponent = connect(mapStateToProps, mapDispatchToProps)(AuthenticatedComponent);
 return Component.AuthenticatedComponent
}

路由上使用

<Router history={browserHistory}>
 <Route path="/admin" component={requireAuthentication(AdminComponent)} />
</Router>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何強制刷新react hooks組件

    如何強制刷新react hooks組件

    這篇文章主要介紹了如何強制刷新react hooks組件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 解決React報錯Rendered more hooks than during the previous render

    解決React報錯Rendered more hooks than during

    這篇文章主要為大家介紹了React報錯Rendered more hooks than during the previous render解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 路由react-router-dom的基本使用教程

    路由react-router-dom的基本使用教程

    在React中,路由是一套映射規(guī)則,是URL路徑與組件的對應(yīng)關(guān)系。使用React路由,就是配置路徑和組件的對應(yīng)關(guān)系,這篇文章主要介紹了路由react-router-dom的使用,需要的朋友可以參考下
    2023-02-02
  • 利用React實現(xiàn)一個有點意思的電梯小程序

    利用React實現(xiàn)一個有點意思的電梯小程序

    這篇文章主要為大家詳解介紹了如何利用React實現(xiàn)一個有點意思的電梯小程序,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-08-08
  • 前端框架react-spring基礎(chǔ)用法

    前端框架react-spring基礎(chǔ)用法

    這篇文章主要為大家介紹了前端框架react-spring基礎(chǔ)用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法)

    React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法)

    i18next?是一款強大的國際化框架,react-i18next?是基于?i18next?適用于?React?的框架,本文介紹了?react-i18next?的基本用法,如果更特殊的需求,文章開頭的官方地址可以找到答案
    2023-01-01
  • react?express實現(xiàn)webssh?demo解析

    react?express實現(xiàn)webssh?demo解析

    這篇文章主要為大家介紹了詳解react?express實現(xiàn)webssh?demo解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • 詳解webpack2+React 實例demo

    詳解webpack2+React 實例demo

    本篇文章主要介紹了詳解webpack2+React 實例demo,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • React Native自定義路由管理的深入理解

    React Native自定義路由管理的深入理解

    路由管理的功能主要指的頁面跳轉(zhuǎn)、goBack、帶參數(shù)跳轉(zhuǎn)等功能,這篇文章主要給大家介紹了關(guān)于React Native自定義路由管理的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • React組件間通訊傳值實現(xiàn)詳解

    React組件間通訊傳值實現(xiàn)詳解

    這篇文章主要介紹了React組件間通訊傳值,react組件的通信屬于開發(fā)基礎(chǔ)知識,今天來梳理一下,當然rudex還按老規(guī)矩排除在外,如同上篇文章的hooks一樣,單獨梳理
    2022-11-11

最新評論