最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React application with users roles - Stack Overflow

programmeradmin0浏览0评论

I started working with React/Redux about 2 months ago and I want to make an application with user roles. When a user logs in I want to change my Route ponent:

<Route path={"/"} ponent={MainPage}></Route>

To ponent

<Route path={"/"} ponent={MainPageUser}></Route>

If admin

<Route path={"/"} ponent={MainPageAdmin}></Route>

But if I make a switch with my Router I get the error

Warning: [react-router] You cannot change <Router routes>; it will be ignored

I want make access based on the type of account.

I started working with React/Redux about 2 months ago and I want to make an application with user roles. When a user logs in I want to change my Route ponent:

<Route path={"/"} ponent={MainPage}></Route>

To ponent

<Route path={"/"} ponent={MainPageUser}></Route>

If admin

<Route path={"/"} ponent={MainPageAdmin}></Route>

But if I make a switch with my Router I get the error

Warning: [react-router] You cannot change <Router routes>; it will be ignored

I want make access based on the type of account.

Share Improve this question edited Jul 24, 2017 at 14:11 Brett DeWoody 63k31 gold badges144 silver badges192 bronze badges asked Jul 24, 2017 at 13:29 Paweł BacaPaweł Baca 8882 gold badges15 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

One option is to create a single ponent for the / path, and within that ponent return a different ponent based on the user role.

<Route path={"/"} ponent={Home} userRole={userRole}></Route>

class Home extends React.Component {
  render() {
    return this.props.userRole === "admin" ? <HomeAdmin /> : <HomeVisitor />;
  }
}

I remend you rootPageComponent wrapping the switchs.

How about this?

(Below is a just simple sample code. Please note that it does not work correctly)

<Route path={"/"} ponent={RootPage}></Route>

// RootPage.js
export default const RootPage({role}) => {
    switch(role) {
        case USER: return <MainPageUser />
        case ADMIN: return <AdminPage />
        default: return <MainPage />
    }
}
发布评论

评论列表(0)

  1. 暂无评论