我正在使用MobX
进行状态管理,并且还将一些信息保存在localStorage
中。
这是我的mobxStore的样子:
const hydrate = create({
storage: localStorage, // or AsyncStorage in react-native; default: localStorage
jsonify: false // if you use AsyncStorage, here shoud be true; default: true
})
export class VendorStore {
@observable vendor = {};
@persist @observable.ref loginState = localStorage.getItem('loginState');
}
var vendorStore = new VendorStore()
hydrate('vendorStore', vendorStore).then(() => console.log('vendorStore has been hydrated'))
export default vendorStore
这个想法是通过
loginState
控制页面的重定向这是我的
Dashboard.js
的样子:if (vendorStore.loginState === false) { return <Redirect to='/signin' /> }
if (vendorStore.loginState === true) {
return (
<div className="Dashboard">
<h1>Dashboard</h1>
<Main />
</div>
)
它要么无法正常工作,或者当
loginState
为false
时无法重定向,或者抛出如下所示的我反应渲染错误:react-dom.development.js:57 Uncaught Invariant Violation: Dashboard(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null;
任何帮助,将不胜感激,
谢谢
最佳答案
If else
在jsx上不起作用,请尝试以下
vendorStore.loginState ? return (
<div className="Dashboard">
<h1>Dashboard</h1>
<Main />
</div>
): return <Redirect to='/signin' />