简体   繁体   English

如何将数据从一个选项卡导航器发送到另一个?

[英]How to send data from one Tab Navigator to another?

I have an a structure like:我有一个像这样的结构:

    <Tab.Navigator
            
            }}>
                <Tab.Screen name="Train" component={Train} options={{
                    tabBarIcon: ({focused}) => (
                        <View>
                            <Image
                            source={focused ? require('../assets/images/train_use.png') : require('../assets/images/train.png') }
                            style={{
                                width: 50,
                                height: 50,
                            }}
                            resizeMode='contain'/>
                        </View>
                    ),
                }}/>
                
                <Tab.Screen name="Result" component={Result} options={{
                tabBarButton: () => null,
                tabBarStyle: { display: "none" }}}/>

                <Tab.Screen name="Statistics" component={Statistics} initialParams={{ wordsInText: 0 }} options={{
                    tabBarIcon: ({focused}) => (
                    ),
                }}/>
                <Tab.Screen name="Profile" component={Profile} options={{
                tabBarIcon: ({focused})>
                ),
                }}/>
            </Tab.Navigator>  

I need to send params from Stack 'Train' into the stack 'Statistics', how can i do it, pls help我需要将参数从 Stack 'Train' 发送到 Stack 'Statistics',我该怎么做,请帮忙

const [count, setCount] = useState(0); // I need to take this value in Train component

//and take it in the Statistics component

I try:我尝试:

routse.params.count 

But it doesn't work.但它不起作用。 Pls, help请帮助

Try to pass count and setCount as components params:尝试将countsetCount作为组件参数传递:

 import {Button, Text, View} from 'react-native'; import {SafeAreaProvider} from 'react-native-safe-area-context'; import {NavigationContainer} from '@react-navigation/native'; import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'; import {useState} from "react"; const Tab = createBottomTabNavigator(); function Train({count, setCount}: { count: number, setCount: Function }) { return ( <View> <Text>Train: {count}</Text> <Button onPress={() => setCount(count + 1)} title={'up'}/> </View> ); } function Result({count, setCount}: { count: number, setCount: Function }) { return ( <View> <Text>Result: {count}</Text> <Button onPress={() => setCount(count + 1)} title={'up'}/> </View> ); } export default function App() { const [count, setCount] = useState(0); return ( <SafeAreaProvider> <NavigationContainer> <Tab.Navigator> <Tab.Screen name="Train" component={() => <Train count={count} setCount={setCount}></Train>} /> <Tab.Screen name="Result" component={() => <Result count={count} setCount={setCount}></Result>} /> </Tab.Navigator> </NavigationContainer> </SafeAreaProvider> ); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在本机反应中将值从一个选项卡发送到另一个选项卡 - How to send value from one tab to another tab in react native 如何从一个屏幕获取数据到另一个屏幕(当前使用 Stack Navigator) - How can I get data from one screen to another (currently using Stack Navigator) 如何从功能组件导航到选项卡导航器中的屏幕,其中该选项卡导航器嵌套在父堆栈导航器中 - How to Navigate from a functional component, from a screen in a tab navigator, where that tab navigator is nested in a parent stack navigator 如何将数据从一个 ejs 文件发送到另一个 - how to send data from one ejs file to another 如何将 JSON 数组数据从一个 JS 函数发送到另一个? - How to send JSON array data from one JS function to Another? 如何将数据从一个窗口发送到另一窗口? - How to send data from one window to another window? 如何在 React js 中将数据从一个网站发送到另一个网站? - How to send data from one website to another website in React js? 如何将数据从一个反应组件发送到另一个反应组件? - How to send data from one react component to another react component? 如何将数据从一个域发送和接收到另一个域? - How to send and recieve data from one domain to another? 如何将数据从一个模块发送到另一个模块? - How can i send data from one module to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM