Answers for "react functional components setstate callback"

7

react callback set staet

setState(  { name: "Michael" },  () => console.log(this.state));// => { name: "Michael" }
Posted by: Guest on November-12-2019
1

react functional components setstate callback

const [state, setState] = useState({ name: "Michael" })
const isFirstRender = useRef(true)

useEffect(() => {
  if (!isFirstRender.current) {
    console.log(state) // do something after state has updated
  }
}, [state])

useEffect(() => { 
  isFirstRender.current = false // toggle flag after first render/mounting
}, [])
Posted by: Guest on November-26-2020

Code answers related to "react functional components setstate callback"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language