【发布时间】:2021-02-08 19:26:07
【问题描述】:
我是新手。
我需要知道为什么我不能调用另一个包含钩子的函数(在一个文件 js 中)?
下面是我的代码:
函数 A
const callService = async () => {
const Loginreducer = useSelector((state) => state.Loginreducer)
const email = Loginreducer.form.email;
const token = Loginreducer.credential.token;
console.log(email, token);}
功能 B
const MyHomeStack = ({ navigation }) => {
return (
<Stack.Navigator
screenOptions={{
animationEnabled: false,
title: "Some App",
headerLeft: () => (
<View style={{ marginHorizontal: 10, flexDirection: "row" }}>
<TouchableOpacity onPress={() => navigation.replace('MyTab')}>
<MaterialCommunityIcons style={styles.navItem} name="fish" size={25} />
</TouchableOpacity>
</View>
),
headerRight: () => (
<View style={{ marginHorizontal: 10, flexDirection: "row" }}>
<TouchableOpacity onPress={() =>
Alert.alert("Confirmation", "Are you sure want to logout?",
[
{
text: 'OK',
onPress: () => callService()
},
{
text: 'Cancel',
},
],
)}>
<MaterialCommunityIcons style={styles.navItem} name="fish-off" size={25} />
</TouchableOpacity>
</View>
)
}}
>
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Food" component={FoodScreen} />
<Stack.Screen name="MyTab" component={MyTab} />
<Stack.Screen name="MyDrawer" component={MyDrawer} />
</Stack.Navigator>
)
}
功能C
const MyTab = () => {
}
...等
如果我将整个 const callService = async () => {} 放在返回标记上方的 const MyHomeStack 内,它会起作用。
但我希望它在 MyHomeStack 函数之外, 因为 callService 函数(函数 A)需要被其他函数(函数 C、D 等)访问,而不仅仅是 MyHomeStack 函数(函数 B)。
有人可以帮忙吗?
谢谢
【问题讨论】:
标签: react-native react-redux react-hooks hook