title: how is the prevItem getting the value of the previous item?
body: <p>i have made a todo app and i am trying to change the state of the array and for that i used an onclick method.can someone explain how it is actually working and is able to log the value of my last items.</p>
  <pre><code>function App() {
  const [inputText, setInputText] = useState(&quot;&quot;);
  const [items, setItems] = useState([]);
  
  function handleChange(event) {
  const newValue = event.target.value;
  setInputText(newValue);
  }
  
  function addItem() {
  setItems(function (prevItems) {
    console.log(prevItems);
    return [...prevItems, inputText];
  });
  setInputText(&quot;&quot;);
   }
  
      return (
      &lt;div className=&quot;container&quot;&gt;
    &lt;div className=&quot;heading&quot;&gt;
      &lt;h1&gt;To-Do List&lt;/h1&gt;
    &lt;/div&gt;
    &lt;div className=&quot;form&quot;&gt;
      &lt;input onChange={handleChange} type=&quot;text&quot; /&gt;
      &lt;button onClick={addItem}&gt;
        &lt;span&gt;Add&lt;/span&gt;
      &lt;/button&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;ul&gt;
        {items.map((todoItem) =&gt; (
          &lt;li&gt;{todoItem}&lt;/li&gt;
        ))}
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;    /div&gt;
    );
     }
  </code></pre>