Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

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
}, [])
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #react #functional #components #setstate #callback
ADD COMMENT
Topic
Name
4+9 =