title: React setState return undefined with function
body: <p>In my application, a token is used to retrieve user's data with a function. Here's it: </p>
  
  <pre><code>export function getUserInfos(token) {
      if (token == "") {
          window.location = "/";
      }
      axios.get(
          url,
          {
              headers: {
                  "Authorization": "Bearer " + token
              }
          }
      ).then((result) =&gt; {
          var surname = result.data.userInfos.surname;
          var name = result.data.userInfos.name;
          var userInfos = {
              surname: surname,
              name: name
          }
          document.title = "Profil - " + surname + " " + name;
          console.log(userInfos)
          return userInfos;
      }).catch((err) =&gt; {
      })
  }
  </code></pre>
  
  <p><code>console.log(userInfos)</code> works fine when the function is called but impossible to get this value with <code>setState()</code>, in the side of my component. Whatever I do I'm still getting <code>undefined</code>. Here's the code in my component:</p>
  
  <pre><code>class HomePage extends Component {
      static propTypes = {
          cookies: instanceOf(Cookies).isRequired
      };
  
      constructor(props) {
          super(props);
  
          const { cookies } = props;
          this.state = {
              token: cookies.get('token')
          }
      };
      componentDidMount() {
          this.setState((state, props) =&gt; {
              return {userInfos: getUserInfos(this.state.token)}
          }, () =&gt; {
              console.log(this.state.userInfos) // 'undefined'
          })
      }
  
      render() {
          console.log(this.state)
          return (
              &lt;div&gt;
                  &lt;h1&gt;MyHomePage&lt;/h1&gt;
              &lt;/div&gt;
          )
      }
  }
  
  export default withCookies(HomePage);
  </code></pre>
  
  <p>I'm sure this is a problem with asynchronous functions but I can't surround where the problem is from or what I'm doing wrong. I also checked and this is not a problem with the importation of my function <code>getUserInfos</code>.</p>