title: React and Redux: Dispatching all actions within one function
body: <p>In my react application, a user logs in and then directed to his/her dashboard upon authentication. In order to display all the data of the user in the dashboard I have a separate file in which there is a function called getUserData() which dispatches all the actions of the dashboard components and then getUserData() is called within loadUser() so every time the app renders it excutes loadUser(). Is this a good practice and an efficient way to display user data as the app grows larger? Or is there a better way to do it and if so, I would love to know about it. Thank you.</p>
  <p>//App.js</p>
  <pre><code>const token = localStorage.getItem('token')
  store.dispatch({type: USER_LOADING});
  if(token){
      store.dispatch({type: USER_AUTHENTICATED})
  }
  
  class App extends Component{
    componentDidMount(){
      store.dispatch(loadUser());
    }
    render(){
    return(
    &lt;Provider store={store}&gt;
    &lt;div className=&quot;App&quot;&gt;
      &lt;Router&gt;
        &lt;Switch&gt;
          &lt;Route exact path=&quot;/login&quot; component={LoginPage}/&gt;
          &lt;ProtectedRoute exact path=&quot;/dashboard&quot; component={Dashboard}/&gt;
         
        &lt;/Switch&gt;
      &lt;/Router&gt;
    &lt;/div&gt;
    &lt;/Provider&gt;
  
  );
  }
  };
  
  export default App;
  </code></pre>
  <p>//Load user</p>
  <pre><code>export const loadUser = () =&gt; (dispatch, getState) =&gt;{
  
  //dispatch(getNetworkList(config))
    axios.get('/api/user/', setAuthorizationHeader(getState))
      .then(res =&gt; {
        dispatch({
        type: USER_LOADED,
        payload: res.data
      })
      dispatch(getUserData())
    })
      .catch(err =&gt; {
        dispatch(returnErrors(err.response.data, err.response.status));
        dispatch({
          type: AUTH_ERROR
        });
      });
    };
  </code></pre>
  <p>//Get user data</p>
  <pre><code>export const getUserData = () =&gt;(dispatch) =&gt;{
          dispatch(userProfile())
          dispatch(getComments())
          dispatch(getInvites())
          dispatch(getNotifications())
          dispatch(getMessages())
          dispatch(getProjects())
          dispatch(getConnections())
          dispatch(getReply())
          dispatch(getPendingRequests())
          dispatch(getAssignedTasks())
      
  };
  </code></pre>