TypeError: undefined is not a function (near '...(0, _react.useCallBack)...'). I don't know what's wrong with the usCallBack() function in my code
TypeError: undefined is not a function (near '...(0, _react.useCallBack)...'). I don't know what's wrong with the usCallBack() function in my code
如果我在我的代码中使用 useCallBack() 并且在删除它之后我的 filterScreen.js 不起作用,我会收到此错误。
我正在尝试使用 react-navigation 获取并保存“save”中的参数,并在控制台中打印过滤器的值。
下面是我的代码:
import React, { useState, useEffect, useCallBack } from "react";
import { View, Text, StyleSheet, Switch } from "react-native";
import { HeaderButtons, Item } from "react-navigation-header-buttons";
import CustomHeaderButton from "../component/CustomHeaderButton";
const Filters = (props) => {
return (
<View style={styles.filterContainer}>
<Text>{props.label}</Text>
<Switch value={props.state} onValueChange={props.Value} />
</View>
);
};
const FiltersScreen = (props) => {
const { navigation } = props;
const [isGlutenFree, setisGlutenFree] = useState(false);
const [isLactoseFree, setisLactoseFree] = useState(false);
const [isVegan, setisVegan] = useState(false);
const [isVeg, setisVeg] = useState(false);
const savedFilters = useCallBack(() => {
const appliedFilters = {
glutenFree: isGlutenFree,
lactoseFree: isLactoseFree,
vegan: isVegan,
veg: isVeg,
};
console.log(appliedFilters);
}, [isGlutenFree, isLactoseFree, isVegan, isVeg]);
useEffect(() => {
navigation.setParams({ save: savedFilters });
}, [savedFilters]);
return (
<View style={styles.screen}>
<Text style={styles.title}>Filter Available!</Text>
<Filters
label="Gluten-Free"
state={isGlutenFree}
Value={(newValue) => setisGlutenFree(newValue)}
/>
<Filters
label="Lactose-Free"
state={isLactoseFree}
Value={(newValue) => setisLactoseFree(newValue)}
/>
<Filters
label="Vegan"
state={isVegan}
Value={(newValue) => setisVegan(newValue)}
/>
<Filters
label="Veg"
state={isVeg}
Value={(newValue) => setisVeg(newValue)}
/>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
alignItems: "center",
},
title: {
fontFamily: "open-sans",
fontSize: 22,
margin: 20,
textAlign: "center",
},
filterContainer: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
width: "80%",
marginVertical: 10,
},
});
FiltersScreen.navigationOptions = (navigationData) => {
return {
headerTitle: "Filter Meals",
headerLeft: () => (
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>
<Item
title="Menu"
iconName="ios-menu"
onPress={() => {
navigationData.navigation.toggleDrawer();
}}
/>
</HeaderButtons>
),
headerRight: () => (
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>
<Item
title="Save"
iconName="ios-save"
onPress={navigationData.navigation.getParam("save")}
/>
</HeaderButtons>
),
};
};
export default FiltersScreen;
我不知道如何解决这个错误以及为什么我会收到这个错误?
发生这种情况是因为 savedFilters
不是函数。
useCallback
没有 return 任何东西。已经更新了 Filters
的代码,如果你可以看看。
我已经使用 useMemo
来计算任何变化的 currentFilters
,每当你的 currentFilters
变化时,它会自动调用 useEffect
进行导航更新。
const FiltersScreen = (props) => {
const { navigation } = props;
const [isGlutenFree, setisGlutenFree] = useState(false);
const [isLactoseFree, setisLactoseFree] = useState(false);
const [isVegan, setisVegan] = useState(false);
const [isVeg, setisVeg] = useState(false);
const selectedFilters = useMemo(() => {
return {
glutenFree,
lactoseFree,
vegan,
veg
};
}, [glutenFree, lactoseFree, vegan, veg])
useEffect(() => {
navigation.setParams({ save: selectedFilters });
}, [selectedFilters]);
return (
<View style={styles.screen}>
<Text style={styles.title}>Filter Available!</Text>
<Filters
label="Gluten-Free"
state={isGlutenFree}
Value={(newValue) => setisGlutenFree(newValue)}
/>
<Filters
label="Lactose-Free"
state={isLactoseFree}
Value={(newValue) => setisLactoseFree(newValue)}
/>
<Filters
label="Vegan"
state={isVegan}
Value={(newValue) => setisVegan(newValue)}
/>
<Filters
label="Veg"
state={isVeg}
Value={(newValue) => setisVeg(newValue)}
/>
</View>
);
};
如果我在我的代码中使用 useCallBack() 并且在删除它之后我的 filterScreen.js 不起作用,我会收到此错误。
我正在尝试使用 react-navigation 获取并保存“save”中的参数,并在控制台中打印过滤器的值。
下面是我的代码:
import React, { useState, useEffect, useCallBack } from "react";
import { View, Text, StyleSheet, Switch } from "react-native";
import { HeaderButtons, Item } from "react-navigation-header-buttons";
import CustomHeaderButton from "../component/CustomHeaderButton";
const Filters = (props) => {
return (
<View style={styles.filterContainer}>
<Text>{props.label}</Text>
<Switch value={props.state} onValueChange={props.Value} />
</View>
);
};
const FiltersScreen = (props) => {
const { navigation } = props;
const [isGlutenFree, setisGlutenFree] = useState(false);
const [isLactoseFree, setisLactoseFree] = useState(false);
const [isVegan, setisVegan] = useState(false);
const [isVeg, setisVeg] = useState(false);
const savedFilters = useCallBack(() => {
const appliedFilters = {
glutenFree: isGlutenFree,
lactoseFree: isLactoseFree,
vegan: isVegan,
veg: isVeg,
};
console.log(appliedFilters);
}, [isGlutenFree, isLactoseFree, isVegan, isVeg]);
useEffect(() => {
navigation.setParams({ save: savedFilters });
}, [savedFilters]);
return (
<View style={styles.screen}>
<Text style={styles.title}>Filter Available!</Text>
<Filters
label="Gluten-Free"
state={isGlutenFree}
Value={(newValue) => setisGlutenFree(newValue)}
/>
<Filters
label="Lactose-Free"
state={isLactoseFree}
Value={(newValue) => setisLactoseFree(newValue)}
/>
<Filters
label="Vegan"
state={isVegan}
Value={(newValue) => setisVegan(newValue)}
/>
<Filters
label="Veg"
state={isVeg}
Value={(newValue) => setisVeg(newValue)}
/>
</View>
);
};
const styles = StyleSheet.create({
screen: {
flex: 1,
alignItems: "center",
},
title: {
fontFamily: "open-sans",
fontSize: 22,
margin: 20,
textAlign: "center",
},
filterContainer: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
width: "80%",
marginVertical: 10,
},
});
FiltersScreen.navigationOptions = (navigationData) => {
return {
headerTitle: "Filter Meals",
headerLeft: () => (
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>
<Item
title="Menu"
iconName="ios-menu"
onPress={() => {
navigationData.navigation.toggleDrawer();
}}
/>
</HeaderButtons>
),
headerRight: () => (
<HeaderButtons HeaderButtonComponent={CustomHeaderButton}>
<Item
title="Save"
iconName="ios-save"
onPress={navigationData.navigation.getParam("save")}
/>
</HeaderButtons>
),
};
};
export default FiltersScreen;
我不知道如何解决这个错误以及为什么我会收到这个错误?
发生这种情况是因为 savedFilters
不是函数。
useCallback
没有 return 任何东西。已经更新了 Filters
的代码,如果你可以看看。
我已经使用 useMemo
来计算任何变化的 currentFilters
,每当你的 currentFilters
变化时,它会自动调用 useEffect
进行导航更新。
const FiltersScreen = (props) => {
const { navigation } = props;
const [isGlutenFree, setisGlutenFree] = useState(false);
const [isLactoseFree, setisLactoseFree] = useState(false);
const [isVegan, setisVegan] = useState(false);
const [isVeg, setisVeg] = useState(false);
const selectedFilters = useMemo(() => {
return {
glutenFree,
lactoseFree,
vegan,
veg
};
}, [glutenFree, lactoseFree, vegan, veg])
useEffect(() => {
navigation.setParams({ save: selectedFilters });
}, [selectedFilters]);
return (
<View style={styles.screen}>
<Text style={styles.title}>Filter Available!</Text>
<Filters
label="Gluten-Free"
state={isGlutenFree}
Value={(newValue) => setisGlutenFree(newValue)}
/>
<Filters
label="Lactose-Free"
state={isLactoseFree}
Value={(newValue) => setisLactoseFree(newValue)}
/>
<Filters
label="Vegan"
state={isVegan}
Value={(newValue) => setisVegan(newValue)}
/>
<Filters
label="Veg"
state={isVeg}
Value={(newValue) => setisVeg(newValue)}
/>
</View>
);
};