[英]App(…): Nothing was returned from render, when routing different roles of user
我使用此功能來檢查已登錄用戶的角色。
export function getUserRole() {
return (
localStorage.getItem('token') &&
jwtDecode(localStorage.getItem('token')).role
)
}
在我的index.js中,我有$(this)
class App extends Component {
constructor(props) {
super(props)
this.role = getUserRole()
}
render() {
if (this.role === 'member') {
return (
<Provider store={userStore}>
<BrowserRouter>
<div className="App">
<Switch>
<Redirect exact from="/" to="/dashboard" />
<Auth path="/dashboard" component={Dashboard} />
<Route path="/login" component={Login} />
</Switch>
</div>
</BrowserRouter>
</Provider>
)
}
}
}
但是我得到這個錯誤?
App(...):渲染未返回任何內容。 這通常意味着缺少return語句。 或者,不渲染任何內容,則返回null。
我不確定是否應該將getUserRole函數放入構造函數。
錯誤確切地說明了字面意思。 既然你已經添加的if (this.role === 'member')
就沒有return
,如果它不是一個成員,必須有一個return
內render
方法
您還應該處理未經身份驗證的方案。因此,在這種情況下,應更新render方法以返回以下內容:
render() {
if (this.role !== 'member') {
return (<div>Unauthenticated</div>);
}
return (
<Provider store={userStore}>
<BrowserRouter>
<div className='App'>
<Switch>
<Redirect exact from='/' to='/dashboard' />
<Auth path='/dashboard' component={Dashboard} />
<Route path='/login' component={Login} />
</Switch>
</div>
</BrowserRouter>
</Provider>
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.