-1

assume i have this code:

const [value, setValue] = useState();

useEffect(() => {
   setvalue('1')
   return () => {
       clearData()
   }
}, []);

const clearData = useCallback(() => {
  console.log('data when unmount', value)
}, [value]);

But i always got 'data when unmount' undefined

Can some one help?

famfamfam
  • 396
  • 3
  • 8
  • 31
  • This looks like the `clearData` callback should have the current `value` state value closed over in scope when the `value` state updates. Think you could create a *running* [codesandbox](https://codesandbox.io/) demo that reproduces the issue we could inspect live? – Drew Reese Oct 21 '22 at 05:04

1 Answers1

-1
const [value, setValue] = useState();

const clearData = useCallback(() => {
    console.log('data when unmount', value)
  }, [value]);

useEffect(() => {
   setvalue('1')
   return () => {
       clearData()
   }
}, [clearData])

Initial this is show as unmount, But View will unmount it shows a value(view go to the back)