简体   繁体   English

同时向多个屏幕传递参数

[英]Passing parameters to multiple screens in the same time

I'm sending some data through my nested navigators.我正在通过我的嵌套导航器发送一些数据。 I've got 2 screens on my tab navigator QR kod and eINFO .我的标签导航QR kodeINFO上有 2 个屏幕。 I'm passing my data to qr kod here我在这里将我的数据传递给qr kod

      navigation.navigate("Drawer", {
        screen: "Home",
        params: {
          screen: "QR kod",
          params: { user: newUser },
        },
      });

these are my navigators这些是我的导航员

  return (
    <Tab.Navigator
      screenOptions={({route}) => ({
        tabBarIcon: ({focused, color, size}) => {
          let iconName;

          if (route.name === 'QR kod') {
            iconName = focused ? 'qrcode' : 'qrcode';
          } else if (route.name === 'eINFO') {
            iconName = focused ? 'infocirlceo' : 'infocirlceo';
          }

          return <AntDesign name={iconName} size={24} color={'black'} />;
        },
      })}
      tabBarOptions={{
        activeTintColor: 'red',
        inactiveTintColor: 'grey',
      }}>
      <Tab.Screen name="QR kod" component={Main} />
      <Tab.Screen name="eINFO" component={Einfo} />
    </Tab.Navigator>
  );
}

function DrawerNav() {
  return (
    <Drawer.Navigator initialRouteName="QR kod">
      <Drawer.Screen name="Home" component={TabNavigation} />
    </Drawer.Navigator>
  );
}

How do I pass the data to both eINFO and QR kod at the same time如何将数据同时传递给eINFOQR kod

Ciao, unfortunately if you are using navigation.navigate to pass data to qr kod screen, you cannot pass same data to eINFO screen at the same time. Ciao,不幸的是,如果您使用navigation.navigate将数据传递到qr kod屏幕,您不能同时将相同的数据传递到eINFO屏幕。 If you want to have the same data in both screens at the same time my suggest is to use a global state manager like react-redux .如果您想同时在两个屏幕上显示相同的数据,我的建议是使用全局 state 管理器,例如react-redux With react-redux you don't need to pass data between screens with navigation.navigate anymore.使用 react-redux,您不再需要使用navigation.navigate在屏幕之间传递数据。 Just dispatch data you need to redux store and then read data dispatched in any screen you need.只需将您需要的数据发送到 redux 存储,然后在您需要的任何屏幕中读取发送的数据。

Using the initialParams prop on your tabs may work.在您的选项卡上使用 initialParams 道具可能会起作用。 It's useful to pass params to child screens automatically.自动将参数传递给子屏幕很有用。

function Home(props) {
  const params = props.route.params;
  return (
    <Tab.Navigator
      screenOptions={({route}) => ({
        tabBarIcon: ({focused, color, size}) => {
          let iconName;
          ...
    }>
      <Tab.Screen name="QR kod" component={Main} initialParams={params} />
      <Tab.Screen name="eINFO" component={Einfo} initialParams={params} />
    </Tab.Navigator>
  );
}

Then your user param will be passed to both screens.然后您的user参数将被传递到两个屏幕。

Change your navigate call to:将您的导航呼叫更改为:

navigation.navigate("Drawer", {
  screen: "Home",
  params: {
    screen: "QR kod",
    user: newUser,
  },
});

I'm not sure exactly how your data flow is structured from what you have posted.我不确定您的数据流是如何根据您发布的内容构建的。 Usually however, something like 'user data' is best handled by Redux as Giovanni Esposito said.然而,正如 Giovanni Esposito 所说,通常情况下,像“用户数据”这样的东西最好由 Redux 处理。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM