gpt4 book ai didi

javascript - react 路由器授权

转载 作者:IT王子 更新时间:2023-10-29 02:55:17 25 4
gpt4 key购买 nike

在安装组件之前进行授权检查的最佳做法是什么?

我使用 react-router 1.x

这是我的路线

React.render((
<Router history={History.createHistory()}>
<Route path="/" component={Dashboard}></Route>
<Route path="/login" component={LoginForm}></Route>
</Router>
), document.body);

这是我的仪表板组件:

var Dashboard = React.createClass({
componentWillMount: function () {
// I want to check authorization here
// If the user is not authorized they should be redirected to the login page.
// What is the right way to perform this check?
},

render: function () {
return (
<h1>Welcome</h1>
);
}
});

最佳答案

更新解决方案React 路由器 v4

<Route 
path="/some-path"
render={() => !isAuthenticated ?
<Login/> :
<Redirect to="/some-path" />
}/>

达到 v3 的 React 路由器

使用“onEnter”事件并在回调中检查用户是否被授权:

<Route path="/" component={App} onEnter={someAuthCheck}>  

const someAuthCheck = (nextState, transition) => { ... }

关于javascript - react 路由器授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32898264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com