React Native为什么setState()函数不更新变量?

icnyk63a  于 2023-02-09  发布在  React
关注(0)|答案(1)|浏览(129)

我尝试在react native with react native navigation中页面成为焦点后立即更新状态,但是如果我记录值,仍然会导致旧的状态。

useFocusEffect(
    React.useCallback(() => {
      if (route.params?.key !== null) {
        setKey(route.params?.key);
        console.log(route.params?.key)              //These two console log give different values
        console.log(key);
        setLastTokenDate(route.params?.date);
      }
      
    }, [route.params?.chiave, route.params?.data]),
  );
cygmwpex

cygmwpex1#

试试这个:

useFocusEffect(
React.useCallback(() => {
if (route.params?.key !== null) {
setKey(route.params?.key);
setLastTokenDate(route.params?.date);
}
}, [route.params?.key, route.params?.date]),
);

useEffect(() => {
console.log(key);
}, [key]);

我想你会发现你错在哪里

相关问题