我有以下代码,该代码为2个组件Login
和List
创建了一个简单的堆栈导航器。我在使用导航器(useState
)的文件中有一个App.js
钩子,我想将setter和getter传递给每个屏幕。我曾尝试在堆栈导航器上使用screenProps
,但在记录传递给每个组件的道具后,变量不会出现。
TL; DR我需要将道具从Stack.Navigator
传递到每个屏幕
<NavigationContainer>
<Stack.Navigator screenProps={{setVariable, variable}}>
<Stack.Screen
name="Login"
component={Login}
options={navOptions}
/>
<Stack.Screen
name="List"
component={List}
options={navOptions}
/>
</Stack.Navigator>
</NavigationContainer>
您需要使用React的上下文API