title: Delay in filter input basic reactJS search function
body: <p>I've made a basic phonebook application for an online exercise that allows you to save people's names and phone numbers to a phone book.  There is also a search input that allows you to filter results based on name. It all works, however, one weird thing that I can't figure out is that the filter is always delayed by one character, the state isn't updated immediately when the user inputs a character into the search bar? </p>
  
  <p>For example: </p>
  
  <p>if I type 'J' into the search it will filter for ''</p>
  
  <p>if I then type 'a' so now 'Ja' is in the search bar it will filter for 'J'</p>
  
  <p>if I then backspace the 'a' so now 'J' is in the search bar it will filter for 'Ja'</p>
  
  <p>and so on... the filter is always one step behind.</p>
  
  <p>I'm new to state's and can't seem to fix it.  Can anyone help me out? thanks.
  (also if there is any other constructive critique to my code that would improve it I'm very open and appreciative of it)</p>
  
  <p>my Code:</p>
  
  <pre><code>const phonebook = [
    {
      id: 1,
      details: {
        name: 'Jim',
        number: '55 748 192'
      }
    },
    {
      id: 2,
      details: {
        name: 'Jason',
        number: '55 111 192'
      }
    },
    {
      id: 3,
      details: {
        name: 'Jonesy',
        number: '55 983 122'
      }
    },
    {
      id: 4,
      details: {
        name: 'Jack',
        number: '0408 729 000'
      }
    },
  
  ]
  
  
  const App = (props) =&gt; {
  
    const [bookData, setBookData] = useState(props.props)
    const [newName, setNewName] = useState('')
    const [newNumber, setNewNumber] = useState('')
    const [theFilter, setTheFilter] = useState('')
    const [namesToShow, setNamesToShow] = useState(bookData.map(person =&gt; person.details))
  
    const [errorMessage, setErrorMessage] = useState('')
  
    console.log('the filter', theFilter, namesToShow)
  
    const addNumber = (event) =&gt; {
      event.preventDefault()
      const numberObject = {
        id: bookData.length + 1,
        details: {
          name: newName,
          number: newNumber,
        }
      }
      if(numberObject.details.name !== "" &amp;&amp; numberObject.details.number !== '') {
        setBookData(bookData.concat(numberObject))
        setNewName('')
        setNewNumber('')
        setErrorMessage('')
      } else {
        if(numberObject.details.name === "" &amp;&amp; numberObject.details.number === "") {
          setErrorMessage('You must enter a name and number!!')
        } else if(numberObject.details.number === "") {
          setErrorMessage('You must enter a number!!')
        } else {
          setErrorMessage('You must enter a name!!!')
        }
      }
    }
  
    const handleFilterChange = (event) =&gt; {
      setTheFilter(event.target.value)
      console.log(theFilter)
      if(theFilter === '') {
        setNamesToShow(bookData.map(person =&gt; person.details))
        console.log('help')
      } else {
        console.log('bookData', bookData.map(person =&gt; person.details))
        setNamesToShow(bookData.map(person =&gt; person.details).filter(person =&gt; person.name.includes(theFilter)))
        console.log('filter', theFilter)
        console.log('potential namestoshow?', bookData.map(person =&gt; person.details).filter(person =&gt; person.name.includes(theFilter)))
        console.log('namestoshow', namesToShow)
      }
    }
  
    const handleNameChange = (event) =&gt; {
      setNewName(event.target.value)
    }
  
    const handleNumberChange = (event) =&gt; {
      setNewNumber(event.target.value)
    }
  
    return (
      &lt;&gt;
        &lt;h1&gt;Phonebook&lt;/h1&gt;
        &lt;div&gt;
          &lt;input value={theFilter} onChange={handleFilterChange} placeholder='Search for a name' /&gt;
          {namesToShow.map(person =&gt; (
            &lt;p&gt;{person.name}: {person.number}&lt;/p&gt;
            ))}
        &lt;/div&gt;
        &lt;form&gt;
          &lt;input value={newName} onChange={handleNameChange} placeholder='Enter a name...'/&gt;
          &lt;input value={newNumber} onChange={handleNumberChange} placeholder='Enter a number...'/&gt;
          &lt;button type='submit' onClick={addNumber}&gt;Save&lt;/button&gt;
        &lt;/form&gt;
        &lt;div&gt;
          {errorMessage}
        &lt;/div&gt;
      &lt;/&gt;
    )
  }
  
  
  
  ReactDOM.render(
    &lt;App props={phonebook} /&gt;,
    document.getElementById('root')
  )
  </code></pre>