1const [userCheck, setUserCheck] = React.useState('');
2
3const userCheckRef = React.useRef(userCheck); // ref to store state value
4
5React.useEffect(() => {
6 userCheckRef.current = userCheck; // save userCheck state value to ref
7}, [userCheck]);
8
9...
10
11useEffect(() => {
12 return () => {
13 dispatch(saveFetchValue(userCheckRef.current)); // pass current ref value
14 };
15}, []);
16