Set State one step behind REACT

Viewed 11

So I am trying to get the input value of the user in my search box but the State is always one step behind. Everytime I click on the button to get the value I get the previous value first then after i click again I get the updated value.

function handleLiClick(e) {
  setsSearch(e.target.textContent)
  handleSubmit(e)
}

I have a form already submitting the text that is getting inputed but the problem is the first input never works the second input does. How do I make it so state gets set right at the click.

I know I have to use a useEffect but I do not know how to put my handleclick function inside the useEffect

useEffect(() => {

  function handleLiClick(e) {
console.log(e.target.textContent)
setsSearch(e.target.textContent)
console.log('BEFORE SUBMIT', search)
handleSubmit(e)
console.log('AFTER SUBMIT', search)
 }
 },[search]) 

my function cannot be called inside the useffect

0 Answers
Related