Home  >  Q&A  >  body text

React functional component does not update state

function Example() {
  const [state, setState] = useState(0)
  
  const someFunction= ()=>{
    ///some task that return a value
    let value = getFunction()
    setState(value)
    console.log(state) //prints 0 ???
  }

  return ( <></> );
}

export default Example;

As you can see, the status has not changed. What can I do to solve this problem. I also tried using useEffect

I tried using useEffect without success

P粉486138196P粉486138196225 days ago487

reply all(1)I'll reply

  • P粉510127741

    P粉5101277412024-03-31 09:04:42

    This is from the React documentation:

    This is also very interesting.

    reply
    0
  • Cancelreply