英文:
React Navigation v6: How do you pass arbitrary props to Tab Navigator?
问题
我在我的React Native应用中使用React Navigation v6有以下选项卡导航器:
<Tab.Navigator>
<Tab.Screen name="Screen1" component={Screen1}/>
<Tab.Screen name="Screen1" component={Screen2}/>
</Tab.Navigator>
我想传递一个名为 prop1
的属性,以便在每个屏幕中访问。我该如何做?
英文:
I have the following Tab Navigator in my React Native app, using React Navigation v6:
<Tab.Navigator>
<Tab.Screen name="Screen1" component={Screen1}/>
<Tab.Screen name="Screen1" component={Screen2}/>
</Tab.Navigator>
I want to pass a prop called prop1
that will be accessible in each screen. How do I do this?
答案1
得分: 1
将组件从component
属性移到屏幕的子组件中,示例:
<Tab.Navigator>
<Tab.Screen name="Screen1">
{() => <Screen1 prop1={prop1} />}
</Tab.Screen>
<Tab.Screen name="Screen2">
{() => <Screen2 prop1={prop1} />}
</Tab.Screen>
</Tab.Navigator>
英文:
You can do this by moving the component from the component
prop to become a child of the screen. Example:
<Tab.Navigator>
<Tab.Screen name="Screen1">
{() => <Screen1 prop1={prop1} />}
</Tab.Screen>
<Tab.Screen name="Screen2">
{() => <Screen2 prop1={prop1} />}
</Tab.Screen>
</Tab.Navigator>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论