So i am using redux, dispatching a token if user logs in. Then I fetch the user's token and if it exists I set X routes available other wise, Y routes. Problem is that at every refresh, the App component renders twice. The first time it token is null second time token is true.
Well, why is it rendering twice? Please answer with different approaches for the same action if you think its more efficient than that.
please, if you need more code, ask.
regards
code:
const App = () => {
const dispatch = useDispatch();
const token = useSelector((state) => state.auth.token);
const onCheckAuth = useCallback(() => dispatch(checkAuth()), [dispatch]);
useEffect(() => {
onCheckAuth();
}, [onCheckAuth]);
const render = () => {
if (token) {
return (
<Switch>
<Route path='/hearing' component={Hearing} />
<Route path='/logout' component={Logout} />
<Route path='/' exact component={Dashboard} />
<Redirect to='/' />
</Switch>
);
}
return (
<Switch>
<Route path='/login' component={Login} />
<Redirect to='/login' />
</Switch>
);
};
console.log(token);
return <>{render()}</>;
};
export default App;