title: How to get data after the first save in react js
body: <p>I have this application:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import React, { useState } from "react";
  
  const App = () =&gt; {
    const [cars, setCars] = useState([]);
    const [car, setCar] = useState({
      name: "",
      color: ""
    });
    const handleChange = e =&gt; {
      setCar({
        ...car,
        [e.target.name]: e.target.value
      });
    };
  
    const addCar = e =&gt; {
      e.preventDefault();
      setCars([...cars, car]);
      setCar({
        car: "",
        color: ""
      });
      console.log(cars);
    };
  
    return (
      &lt;div className="car"&gt;
        &lt;form onSubmit={addCar}&gt;
          &lt;input
            value={car.name}
            onChange={handleChange}
            name="name"
            placeholder="add car name"
          /&gt;
          &lt;input
            value={car.color}
            onChange={handleChange}
            name="color"
            placeholder="add car color"
          /&gt;
          &lt;button type="submit"&gt;Add a car&lt;/button&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    );
  };
  
  export default App;</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>The issue appears when i want to save data clicking on <code>save</code> button. To get the data in <code>console.log(cars);</code>, i have to click twice.
  <br>
  Why this is happening and how to solve this?<br>demo:<a href="https://codesandbox.io/s/falling-fog-jstgi?file=/src/App.js:436-454" rel="nofollow noreferrer">https://codesandbox.io/s/falling-fog-jstgi?file=/src/App.js:436-454</a></p>