title: Private Route with Redux and react router
body: <p>I have a simple app with auth and private route, I want to get data from the server if the user has token, the back end is ready and works fine, and I log in I have data in redux about the user, but I don't know how to handle with refresh page, where should I do dispatch to call action? if I do it in privateRoute.js, it works strange I want to call server ones, but I did it 3-4 times.</p>
  <p>Here are my components without calling <code>sessionActions</code>, <code>sessionActions</code> should update <code>loggedIn</code> and the user would go to <code>/login</code> pages</p>
  <p><code>PrivateRoute</code></p>
  <pre class="lang-js prettyprint-override"><code>import React, { useState, useEffect } from 'react';
  import { Route, Redirect } from 'react-router-dom';
  import PropTypes from 'prop-types';
  import  sessionAction  from '../store/actions/sessionAction';
  
  const PrivateRoute = ({ component: Component, ...rest }) =&gt; {
    const { path, dispatch, loggedIn } = rest;
    });
  
    return (
      &lt;Route
        path={path}
        render={(props) =&gt; (loggedIn ? &lt;Component {...props} /&gt;
       &lt;Component {...props}/&gt;
          : (&lt;Redirect to=&quot;/login&quot; /&gt;))}
      /&gt;
    );
  };
  
  PrivateRoute.propTypes = {
    component: PropTypes.func.isRequired,
  };
  export default PrivateRoute;
  </code></pre>
  <p><code>sessionAction</code></p>
  <pre class="lang-js prettyprint-override"><code>const sessionAction = (path) =&gt; (dispatch) =&gt; {
    return sessionServices(path)
      .then((response) =&gt; {
        console.log(response);
        const { data } = response;
        if (!data.error) {
          dispatch(success(data));
        }
        dispatch(failure(data.error.text));
      })
      .catch((error) =&gt; error);
  };
  </code></pre>
  <p><code>sessionService</code></p>
  <pre class="lang-js prettyprint-override"><code>import axios from 'axios';
  
  axios.defaults.withCredentials = true;
  
  const sessionServices = (path) =&gt; axios({
    method: 'post',
    url: `http://localhost:4000/api/pages${path}`,
  })
    .then((response) =&gt; response)
    .catch((error) =&gt; console.log(error));
  
  export default sessionServices;
  </code></pre>