My application has 2 side, first side is the client app, second side is admin dashboard app. To make it DRY I decided to pass the store in the provider base on user role, so that I don't have to have 2 set of router.
class App extends Component {
constructor(props) {
super(props)
this.role = getUserRole() //get role from localstorage
}
renderSwitchProvider = store => (
<Provider store={store}>
<BrowserRouter>
<div className="App">
<Switch>
<Redirect exact from="/" to="/dashboard" />
<Auth path="/dashboard" component={Dashboard} />
<Route path="/login" component={Login} />
<Route path="/signup" component={Signup} />
</Switch>
</div>
</BrowserRouter>
</Provider>
)
render() {
if (this.role === 'member') {
return this.renderSwitchProvider(store)
} else if (this.role === 'admin') {
return this.renderSwitchProvider(adminStore)
}
return null // problem is here
}
}
export default App
But I got error of
Could not find "store" in either the context or props of "Connect(LoginForm)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(LoginForm)".
My getUserRole does this
export function getUserRole() {
return (
localStorage.getItem('token') &&
jwtDecode(localStorage.getItem('token')).role
)
}