【发布时间】:2020-06-11 19:39:39
【问题描述】:
这是我在 StackOverflow 上的第一篇文章,如果我没有遵循正确的格式,敬请见谅。
我正在使用 tab navigator from React Navigation v 5.x 构建我的第一个应用程序,并且在将道具从一个屏幕传递到另一个屏幕时遇到了一个大问题
我想要实现的是:
- 在我的一个屏幕中更改数据列表。
- 这些更改是否会影响另一个屏幕上发生的事情。
我尝试过this(我未能设置要传递的道具)、this(已弃用的 react-navigation 版本)和this(也是旧版本的 react-navigation)。
和 Redux,但当前版本的反应导航没有可用的示例。
我已经走到了尽头,真的需要一步一步来了解如何实现这一目标。 这是我想要做的粗略草图:
我的想法是通过回调将父状态作为道具发送下来,但是我找不到通过最新版本的反应导航中的屏幕组件发送道具的方法......
这是我的导航设置:
const Tab = createBottomTabNavigator()
export default class MyApp extends Component{
constructor(props) {
super(props);
}
render(){
return (
<NavigationContainer>
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'My tests') {
iconName = focused ? 'ios-list-box' : 'ios-list';
} else if (route.name === 'Testroom') {
iconName = focused ? 'ios-body' : 'ios-body';
}
return <Ionicons name={iconName} size={size} color={color} />;
},
})}
tabBarOptions={{
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
}}>
<Tab.Screen name="My tests" component={MyTests}/> //This is where I want to send props
<Tab.Screen name="Testroom" component={TestRoom} /> //This is where I want to send props
</Tab.Navigator>
</NavigationContainer>
)
}
}
我读过this,但这对我来说毫无意义。这如何适合组件树?什么去哪里?请帮忙!
【问题讨论】:
-
你运气好吗?我在同一条船上,不知道该怎么做。
-
@CaseSilva 看看这是否对您有帮助stackoverflow.com/a/61112784/11725995
-
不是真的,因为我需要创建使用主 App 组件中数据的组件,我认为不应该在那里显式创建它。需要状态值的组件还需要与其父组件通信,该父组件位于堆栈/选项卡导航树的下方。
-
我遇到了非常相似的情况,最终使用了 React 的 Context API。检查这是否适合您的需求:stackoverflow.com/a/61196113/6581190
标签: reactjs react-native react-redux parameter-passing react-props