After login I am not redirecting to my users page I am using react metronic theme. I think something is wrong with props.history.push() Here what I have done so far.
Main component is below:
ReactDOM.render(
<MetronicI18nProvider>
<MetronicLayoutProvider>
<MetronicSubheaderProvider>
<MetronicSplashScreenProvider>
<ToastContainer/>
<App store={store} persistor={persistor} />
</MetronicSplashScreenProvider>
</MetronicSubheaderProvider>
</MetronicLayoutProvider>
</MetronicI18nProvider>,
document.getElementById("root")
);
App component:
export default function App({ store, persistor }) {
return (
/* Provide Redux store */
<Provider store={store}>
{/* Asynchronously persist redux stores and show `SplashScreen` while it's loading. */}
<PersistGate persistor={persistor} loading={<LayoutSplashScreen />}>
{/* Add high level `Suspense` in case if was not handled inside the React tree. */}
<React.Suspense fallback={<LayoutSplashScreen />}>
{/* Override `basename` (e.g: `homepage` in `package.json`) */}
<BrowserRouter>
{/*This library only returns the location that has been active before the recent location change in the current window lifetime.*/}
<MaterialThemeProvider>
{/* Provide `react-intl` context synchronized with Redux state. */}
<I18nProvider>
{/* Render routes with provided `Layout`. */}
<Routes />
</I18nProvider>
</MaterialThemeProvider>
</BrowserRouter>
</React.Suspense>
</PersistGate>
</Provider>
);
}
This is my routers component
<Switch>
{getToken()==null? (
<Route>
<AuthPage/>
</Route>
) : (
<Redirect from="/auth" to="/"/>
)}
<Route path="/error" component={ErrorsPage}/>
{getToken()==null ? (
<Redirect to="/auth/login"/>
) : (
<Layout>
<BasePage/>
</Layout>
)}
</Switch>
this is my base component
export default function BasePage() {
return (
<Suspense fallback={<LayoutSplashScreen/>}>
<Switch>
<Redirect exact from="/" to="/allUsers"/>
<ContentRoute path="/allUsers" component={AllUsers}/>
<Redirect to="error/error-v1"/>
</Switch>
</Suspense>
);
}
this is action which I fire after sign In button
props.onLogin(values).then((res) => {
toast.success('Login Successfull')
props.history.push('/allUsers')
}).catch((err) => {
console.log('error', err)
toast.error('Login failed')
})
and I have same AuthPage component.
api is fired success fully, token is saved successfully and toaster is shown 'Login successful'. But I am not redirecting to my desired page. But if I refresh then I am redirected to home page.
Can anyone help me with this
** history.push is working in /allUsers
route. But not working in Login component
Update: I think I found what I am doing wrong. I don't need to perform any history.push() action when I will save token(which is I am doing in my onLogin action) it should automatic redirect to AllUsers Page. I changed my approach to as below and now it is working.
const {isAuthorized} = useSelector(
({mainAuth}) => ({
isAuthorized: mainAuth.user != null,
}),
shallowEqual
);
replaced getToken with isAuthorized
But history.push() not working here is still mystery.
I think in your case you are not getting history reference in props. You can try these two solutions.
withRouter
fromreact-router
&useHistory
fromreact-router-dom
.