gpt4 book ai didi

javascript - useCallback Hooks 获取旧状态值而不更新

转载 作者:行者123 更新时间:2023-12-03 21:14:26 25 4
gpt4 key购买 nike

我的回调在一次又一次调用后返回相同的状态

我是 react 类中的 react 钩子(Hook)的新手,我本可以使用 shouldcomponentupdate 并且可以解决这个问题

但是 usecallback 钩子(Hook)中没有参数

import React, {
useLayoutEffect,
useState,
useCallback,
useEffect,
} from "react";
import { StyleSheet, Text, View, Platform, YellowBox } from "react-native";
import { HeaderButtons, Item } from "react-navigation-header-buttons";
import HeaderButton from "../components/HeaderButton";
import { Switch } from "react-native-paper";
import Colors from "../constants/Colors";
//check use callback value first

YellowBox.ignoreWarnings([
"Non-serializable values were found in the navigation state",
]);

const FilterSwitch = ({ label, state, onChange }) => (
<View style={styles.filterContainer}>
<Text>{label}</Text>
<Switch
value={state}
trackColor={{ true: Colors.primaryColor }}
thumbColor={Platform.OS === "android" ? Colors.primaryColor : ""}
onValueChange={onChange}
/>
</View>
);

const FiltersScreen = ({ navigation }) => {
const [isGlutenFree, setIsGlutenFree] = useState(false);
const [isLactoseFree, setIsLactoseFree] = useState(false);
const [isVegan, setIsVegan] = useState(false);
const [isVegetarian, setIsVegetarian] = useState(false);

const saveFilters = useCallback(() => {
console.log(isGlutenFree);
const appliedFilters = {
glutenFree: isGlutenFree,
lactoseFree: isLactoseFree,
vegan: isVegan,
isVegetarian: isVegetarian,
};

console.log(appliedFilters);
}, [isGlutenFree, isLactoseFree, isVegan, isVegetarian]);


useLayoutEffect(() => {
navigation.setOptions({
headerTitle: "Filter Meals",
headerLeft: () => (
<HeaderButtons HeaderButtonComponent={HeaderButton}>
<Item
title="Menu"
iconName="ios-menu"
onPress={() => {
navigation.toggleDrawer();
}}
/>
</HeaderButtons>
),
headerRight: () => (
<HeaderButtons HeaderButtonComponent={HeaderButton}>
<Item
title="Save"
iconName="ios-save"
onPress={() => saveFilters()}
/>
</HeaderButtons>
),
});
}, [navigation]);
return (
<View style={styles.screen}>
<Text style={styles.title}>Available Filters / Restrictions</Text>
<FilterSwitch
label="Gluten Free"
state={isGlutenFree}
onChange={(newValue) => {
setIsGlutenFree(newValue);
}}
/>
<FilterSwitch
label="Lactos Free"
state={isLactoseFree}
onChange={(newValue) => setIsLactoseFree(newValue)}
/>
<FilterSwitch
label="Vegan Free"
state={isVegan}
onChange={(newValue) => setIsVegan(newValue)}
/>
<FilterSwitch
label="Vegetarian Free"
state={isVegetarian}
onChange={(newValue) => setIsVegetarian(newValue)}
/>
</View>
);
};

const styles = StyleSheet.create({
screen: {
flex: 1,
alignItems: "center",
},
filterContainer: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
width: "80%",
marginVertical: 15,
},
title: {
fontFamily: "open-sans-bold",
fontSize: 22,
margin: 20,
textAlign: "center",
},
});
export default FiltersScreen;

我怎么解决这个问题?
我已阅读官方文档,但在那里找不到任何相关问题

最佳答案

您的代码的问题是,即使您提供了 useCallback对于所有依赖数组,您只使用 first closure value onPress={() => saveFilters()} 中的函数因为这段代码是在 useLayoutEffect 内部执行的并且仅在 navigation 上运行改变
这里的解决方案是 update navigation options两个navigation改变和开启saveFilters改变

useLayoutEffect(() => {
navigation.setOptions({
headerTitle: "Filter Meals",
headerLeft: () => (
<HeaderButtons HeaderButtonComponent={HeaderButton}>
<Item
title="Menu"
iconName="ios-menu"
onPress={() => {
navigation.toggleDrawer();
}}
/>
</HeaderButtons>
),
headerRight: () => (
<HeaderButtons HeaderButtonComponent={HeaderButton}>
<Item
title="Save"
iconName="ios-save"
onPress={() => saveFilters()}
/>
</HeaderButtons>
),
});
}, [navigation, saveFilters]);

P.S. This is the kind of situation where I feel hooks implementation sometimes becomes hacky or inefficient. Also debuggingclosures is way more difficult then debugging context(this inclass component)

关于javascript - useCallback Hooks 获取旧状态值而不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61709542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com