title: React useEffect() state isn't updated
body: <p>when Im using the useEffect hook with the setState hook, the state isn't updated if I only want the useEffect to run once (i.e. pass an empty array as the second parameter).</p>
  <p>If i pass the array that Im updating as the second parameter, the state gets updated but then the useEffect keeps running in an infinite loop...</p>
  <p>Here is the code that Im trying to run:
  In this example when printing out memes or endPage - they still hold their initial state values ([] and 0)</p>
  <pre><code>function ImageContainer() {
    const [memes, setMemes] = useState([]);
    const [currentIndex, setIndex] = useState(0);
    const [currentPage, setPage] = useState(1);
    const [endPage, setEndPage] = useState(0);
  
    useEffect(() =&gt; {
      getMemes(currentPage).then((data) =&gt; {
        setEndPage(data.pagination.totalPages);
        setMemes(data.data);
        console.log(data);
        console.log(memes);
        console.log(endPage);
      });
    }, []);
  </code></pre>
  <p><a href="https://i.stack.imgur.com/nzrkd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nzrkd.png" alt="enter image description here" /></a></p>
  <p>but if I pass memes as the second parameter:</p>
  <pre><code>function ImageContainer() {
    const [memes, setMemes] = useState([]);
    const [currentIndex, setIndex] = useState(0);
    const [currentPage, setPage] = useState(1);
    const [endPage, setEndPage] = useState(0);
  
    useEffect(() =&gt; {
      getMemes(currentPage).then((data) =&gt; {
        setEndPage(data.pagination.totalPages);
        setMemes(data.data);
        console.log(data);
        console.log(memes);
        console.log(endPage);
      });
    }, [memes]);
  </code></pre>
  <p>Then the console.log shows that both of the states have been updated, but the code keeps looping infinitely.</p>
  <p><a href="https://i.stack.imgur.com/JOAeG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JOAeG.png" alt="enter image description here" /></a></p>
  <p>Thanks!</p>