I want to dispatch an action whenever any page or component of my app loads. So what I am trying to do is to dispatch the action using store.dispatch inside useEffect hook in main App.js file.
When I try, it gives a Maximum update depth exceeded Error
I have seen it done in a tutorial or two, I just cant find what I am doing wrong. Is there any other way of achieving this? Please take a look!
Here is my App.js
import React, { useEffect } from "react"; import { Provider } from "react-redux"; import { BrowserRouter, Route, Routes } from "react-router-dom"; import Login from "./pages/Login"; import Landing from "./pages/Landing"; import Contact from "./pages/Contact"; import Dashboard from "./pages/Dashboard"; import store from "./store"; import { loadUser } from "./tasks/authT"; import setAuthToken from "./utils/setAuthToken"; import PrivateRoutes from "./utils/PrivateRoutes"; if (localStorage.token) { setAuthToken(localStorage.token); } const App = () => { useEffect(() => { store.dispatch(loadUser()); }, []); return ( // <Landing /> <Provider store={store}> <BrowserRouter> <Routes> <Route exact path="/" element={<Landing />} /> <Route exact path="/contact" element={<Contact />} /> <Route exact path="/login" element={<Login />} /> <Route exact path="/dashboard" element={ <PrivateRoutes> <Dashboard /> </PrivateRoutes> } /> </Routes> </BrowserRouter> </Provider> ); }; export default App; this is the action I am trying to dispatch
export const loadUser = () => async (dispatch) => { if (localStorage.token) { setAuthToken(localStorage.token); } try { const res = await axios.get("/api/auth"); dispatch({ type: USER_LOADED, payload: res.data, }); } catch (error) { dispatch({ type: AUTH_ERROR, }); } };