title: React.js: Fetching Data from API with Bearer Token
body: <p>I am building a React.js SPA, that receives data from an API with Bearer Token Authentication (so a fetch request for authentication has to be done first and the given token can be used temporarily to access the data until the auth session ends)</p>
  <p>I can authorize and print the object to the console, but the object is not saved into the variable objectOutput in order to be accessed by the components below.</p>
  <p>My thought is, that the asynchronous fetching causes this, and the components are rendered before the fetch() result has arrived. But I wonder how this could be fixed, so I can store it in the variable and the components can access the data as their props.</p>
  <pre><code>import Header from &quot;./components/Header&quot;;
  import Cardsection from &quot;./components/Cardsection&quot;;
  import Flagsection from &quot;./components/Flagsection&quot;;
  import Footer from &quot;./components/Footer&quot;;
  import MapChart from &quot;./components/MapChart&quot;;
  import { useEffect } from &quot;react&quot;;
  
  let api_url = &quot;https://api-url:3000&quot;;
  let batch_id = &quot;XXX&quot;;
  let api_token = &quot;&quot;;
  let objectOutput = &quot;&quot;;
  
  function App() {
    useEffect(() =&gt; {
  
      const authorize = async () =&gt; {
        var myHeaders = new Headers();
        myHeaders.append(&quot;Content-Type&quot;, &quot;application/json&quot;);
  
        var raw = JSON.stringify({
          username: &quot;username&quot;,
          password: &quot;password&quot;,
        });
  
        var requestOptions = {
          method: &quot;POST&quot;,
          headers: myHeaders,
          body: raw,
          redirect: &quot;follow&quot;
        };
  
        const res = await fetch(api_url + &quot;/auth&quot;, requestOptions).catch(console.log(&quot;error&quot;));
        const data = await res.json();
        api_token = data.token;
        console.log(&quot;Successful Authorization. Token: &quot; + api_token);
  
        const getObject = async () =&gt; {
          var myHeaders = new Headers();
          myHeaders.append(&quot;Authorization&quot;, &quot;Bearer &quot; + api_token);
  
          var requestOptions = {
            method: &quot;GET&quot;,
            headers: myHeaders,
            redirect: &quot;follow&quot;
          };
  
          const res = await fetch(api_url + &quot;/get?function=readObject&amp;args=&quot; + batch_id, requestOptions).catch(console.log(&quot;error&quot;));
          const data = await res.json();
  
          objectOutput = data;
          console.log(data);
  
        
        };
        getObject()
  
      };
  
      authorize();
  
    }, []);
  
    return (
      &lt;div className=&quot;App&quot;&gt;
        &lt;Header
          title=&quot;Lorem Ipsum&quot;
          description=&quot;Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.&quot;
        /&gt;
        &lt;Flagsection hasFlag={objectOutput.alarmFlag} /&gt;
        &lt;Cardsection sort={&quot;&quot;} treatment={&quot;&quot;} plantheight={&quot;&quot;} harvesttime={&quot;&quot;} /&gt;
        &lt;MapChart/&gt;
        &lt;Footer /&gt;
      &lt;/div&gt;
    );
  }
  
  export default App;```
  </code></pre>