title: Get value from fetch in React
body: <p>I try to pass an array of object from localhost:5000/users to Table component as a prop but I can't.</p>
  
  <p>I can fetch data from localhost:5000/users and when I try to do console.log inside it, I can see data. But when I try to do console.log outside fetch function, it returns an empty array.</p>
  
  <p>The question is how can I pass the data to Table component if the data is not visible outside the fetch function ?</p>
  
  <pre><code>import React from 'react';
  import './App.css';
  import Table from './Table';
  
  function App() {
  
      let obj = [];
  
      fetch('http://localhost:5000/users')
      .then((response) =&gt; {
          return response.json();
      })
      .then((data) =&gt; {
          return obj = data;
      })
      .then(() =&gt; {
          console.log(obj); // Here it returns correct data from localhost:5000/users
          return obj;
      });
  
      console.log(obj); // But right here, it returns an empty array
  
      return (
          &lt;div&gt;
              &lt;Table data={obj} /&gt; {/* The question is how can I pass data from localhost:5000/users to Table component ? */}
          &lt;/div&gt;
      )
  }
  
  export default App;
  </code></pre>