title: React hooks includes() for checking if a value exists in array gives one wrong answer every time
body: <p>I have a simple phonebook, which should check if the name exists in the &quot;person&quot; array. I used person.map and includes(). Can anyone explain why this code gives one time &quot;false&quot;, when the value should be &quot;true&quot;, i.e. the name exists in the person array? It does it every time, but only once.</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>// Get a hook function
  const {useState} = React;
  
  const App = () =&gt; {
    const [person, setPerson] = useState([{ name: "John Doe" }]);
    const [newName, setName] = useState("");
  
    const addName = (event) =&gt; {
      event.preventDefault();
      const nameObject = { name: newName };
  
      let mapped = person.map((ele) =&gt; ele.name);
      let found = mapped.includes(newName);
      console.log("found it? ", found);
    };
    const handleNameChange = (event) =&gt; {
      setName(event.target.value);
    };
  
    return (
      &lt;form onSubmit={addName}&gt;
        &lt;div&gt;
          name
          &lt;input value={newName} onChange={handleNameChange} /&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;button type="submit"&gt;add&lt;/button&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    );
  };
  
  ReactDOM.render(
    &lt;App /&gt;,
    document.getElementById("root")
  )</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"&gt;&lt;/script&gt;
  &lt;div id="root"/&gt;</code></pre>
  </div>
  </div>
  </p>
  <p><a href="https://i.stack.imgur.com/UKfYU.png" rel="nofollow noreferrer">enter image description here</a></p>