第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在React Router 4中實(shí)現(xiàn)經(jīng)過(guò)身份驗(yàn)證的路由?

如何在React Router 4中實(shí)現(xiàn)經(jīng)過(guò)身份驗(yàn)證的路由?

森林海 2019-12-12 12:59:10
我試圖實(shí)現(xiàn)經(jīng)過(guò)身份驗(yàn)證的路由,但是發(fā)現(xiàn)React Router 4現(xiàn)在阻止了它的工作:<Route exact path="/" component={Index} /><Route path="/auth" component={UnauthenticatedWrapper}>    <Route path="/auth/login" component={LoginBotBot} /></Route><Route path="/domains" component={AuthenticatedWrapper}>    <Route exact path="/domains" component={DomainsIndex} /></Route>錯(cuò)誤是:警告:您不應(yīng)使用<Route component>和<Route children>在同一路線上;<Route children>將被忽略在這種情況下,實(shí)現(xiàn)此目標(biāo)的正確方法是什么?它出現(xiàn)在react-router(v4)文檔中,提示類似<Router>    <div>    <AuthButton/>    <ul>        <li><Link to="/public">Public Page</Link></li>        <li><Link to="/protected">Protected Page</Link></li>    </ul>    <Route path="/public" component={Public}/>    <Route path="/login" component={Login}/>    <PrivateRoute path="/protected" component={Protected}/>    </div></Router>但是在將一堆路線組合在一起時(shí)是否有可能實(shí)現(xiàn)這一目標(biāo)?更新好吧,經(jīng)過(guò)一番研究,我想到了這個(gè):import React, {PropTypes} from "react"import {Route} from "react-router-dom"export default class AuthenticatedRoute extends React.Component {  render() {    if (!this.props.isLoggedIn) {      this.props.redirectToLogin()      return null    }    return <Route {...this.props} />  }}AuthenticatedRoute.propTypes = {  isLoggedIn: PropTypes.bool.isRequired,  component: PropTypes.element,  redirectToLogin: PropTypes.func.isRequired}發(fā)出錯(cuò)誤的動(dòng)作是正確的render()感覺(jué)。似乎確實(shí)不正確,componentDidMount還是帶有其他掛鉤?
查看完整描述

3 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

您將要使用該Redirect組件。有幾種不同的方法可以解決此問(wèn)題。我喜歡的一個(gè)是,有一個(gè)PrivateRoute組件,該組件接受一個(gè)authed道具,然后根據(jù)該道具進(jìn)行渲染。


function PrivateRoute ({component: Component, authed, ...rest}) {

  return (

    <Route

      {...rest}

      render={(props) => authed === true

        ? <Component {...props} />

        : <Redirect to={{pathname: '/login', state: {from: props.location}}} />}

    />

  )

}

現(xiàn)在你Route的可以看起來(lái)像這樣


<Route path='/' exact component={Home} />

<Route path='/login' component={Login} />

<Route path='/register' component={Register} />

<PrivateRoute authed={this.state.authed} path='/dashboard' component={Dashboard} />

如果您仍然感到困惑,我寫(xiě)了這篇文章可能會(huì)有所幫助 -React Router v4的受保護(hù)路由和身份驗(yàn)證


查看完整回答
反對(duì) 回復(fù) 2019-12-12
?
鳳凰求蠱

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊

Tnx Tyler McGinnis提供解決方案。我是根據(jù)Tyler McGinnis的想法提出的。


const DecisionRoute = ({ trueComponent, falseComponent, decisionFunc, ...rest }) => {

  return (

    <Route

      {...rest}


      render={

        decisionFunc()

          ? trueComponent

          : falseComponent

      }

    />

  )

}

您可以這樣實(shí)現(xiàn)


<DecisionRoute path="/signin" exact={true}

            trueComponent={redirectStart}

            falseComponent={SignInPage}

            decisionFunc={isAuth}

          />

DecisionFunc只是一個(gè)返回true或false的函數(shù)


const redirectStart = props => <Redirect to="/orders" />


查看完整回答
反對(duì) 回復(fù) 2019-12-12
  • 3 回答
  • 0 關(guān)注
  • 1191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)