React Navigation v6:向屏幕传递附加属性的最佳方法?

huangapple go评论56阅读模式
英文:

React Navigation v6: best way to pass additional props to screen?

问题

在React Navigation v6文档的传递额外属性部分中,它说:“有时我们可能想要向屏幕传递额外的属性。我们可以用两种方法做到这一点”,它建议的第一种方法是React Context。我从未使用过React Context,但链接显示它已过时。有没有React Context的更新替代方案?它建议的另一选项是:“使用呈现回调而不是指定组件属性来为屏幕设置”。基本上,我想做的是:

<Tab.Navigator>
  <Tab.Screen prop1={prop1}>
</Tab.Navigator>

但我知道不能以这种方式传递prop1

英文:

In the docs for React Navigation v6, in the Passing Additional Props section it says Sometimes we might want to pass additional props to a screen. We can do that with 2 approaches and the first approach it recommends is React Context. I've never used React Context, but the link says it's out of date. Is there a more up-to-date alternative to React Context? The other option it suggests is: Use a render callback for the screen instead of specifying a component prop.

Basically I want to do:

&lt;Tab.Navigator&gt;
  &lt;Tab.Screen prop1={prop1}&gt;
&lt;/Tab.Navigator&gt;

but I know you can't pass prop1 this way.

答案1

得分: 1

以下是翻译好的部分:

Basically I want to do:

<Tab.Navigator>
  <Tab.Screen prop1={prop1}>
</Tab.Navigator>

but I know you can't pass prop1 this way.

基本上我想要做的是:

<Tab.Navigator>
  <Tab.Screen prop1={prop1}>
</Tab.Navigator>

但我知道你不能以这种方式传递 prop1。

You can achieve what you're looking to do by passing the component you'd like to render as a child of Tab.Screen, as shown below:

你可以通过将要渲染的组件作为 Tab.Screen 的子元素来实现你想要的效果,如下所示:

<Tab.Navigator>
  <Tab.Screen name='some name'>
    {(props) => <YourReactComponentHere {...props} prop1={prop1} />}
  </Tab.Screen>
</Tab.Navigator>

as shown in the React navigation documentation you linked. At some point, I ran into issues with that approach that I resolved by just explicitly passing a "children" prop (I couldn't tell you why this mattered, and may have just been a syntax issue on my part), but most linters will yell at you for that. If you're curious though, that looks like this:

如您所链接的 React 导航文档所示。在某些情况下,我遇到了使用该方法时的问题,我通过显式传递一个 "children" 属性来解决了这个问题(我无法告诉您为什么这很重要,可能只是我语法上的问题),但大多数 lint 工具会警告您。如果您感兴趣,这是它的样子:

<Tab.Navigator>
  <Tab.Screen
    name='some name'
    children={(props) => <YourReactComponentHere {...props} prop1={prop1} />}
  />
</Tab.Navigator>

One of the above should work for you, but as mentioned in the documentation, using context is the preferred method.

上述其中一个应该适用于您,但如文档中所述,使用上下文是首选方法。

I've never used React Context, but the link says it's out of date. Is there a more up-to-date alternative to React Context?

我从未使用过 React Context,但链接说它已经过时了。是否有更加现代的替代方法来使用 React Context?

If you've never used Context, it's a great skill to pick up! You can think of it like a globally scoped state that you can access from within any of your components. Here's a link to the new react documentation on context:

如果您从未使用过 Context,那么学会它是一个很好的技能!您可以将其视为一种全局范围的状态,可以从任何组件内部访问。这是关于 context 的新 React 文档链接:

Passing Data Deeply with Context

You can also use route parameters as Harsh mentioned. But, I typically reserve route parameters for passing information about how I'm navigating to the desired screen.

您还可以像 Harsh 提到的那样使用路由参数。但是,我通常将路由参数保留用于传递有关如何导航到所需屏幕的信息。

英文:

> Basically I want to do:
>
> <Tab.Navigator>
> <Tab.Screen prop1={prop1}>
> </Tab.Navigator>
>
> but I know you can't pass prop1 this way.

You can achieve what you're looking to do by passing the component you'd like to render as a child of Tab.Screen, as shown below:

&lt;Tab.Navigator&gt;
  &lt;Tab.Screen name=&#39;some name&#39;&gt;
    {(props) =&gt; &lt;YourReactComponentHere {...props} prop1={prop1} /&gt;}
  &lt;/Tab.Screen&gt;
&lt;/Tab.Navigator&gt;

as shown in the React navigation documentation you linked. At some point, I ran into issues with that approach that I resolved by just explicitly passing a "children" prop (I couldn't tell you why this mattered, and may have just been a syntax issue on my part), but most linters will yell at you for that. If you're curious though, that looks like this:

&lt;Tab.Navigator&gt;
  &lt;Tab.Screen
    name=&#39;some name&#39;
    children={(props) =&gt; &lt;YourReactComponentHere {...props} prop1={prop1} /&gt;}
  /&gt;
&lt;/Tab.Navigator&gt;

One of the above should work for you, but as mentioned in the documentation, using context is the preferred method.

> I've never used React Context, but the link says it's out of date. Is there a more up-to-date alternative to React Context?

If you've never used Context, it's a great skill to pick up! You can think of it like a globally scoped state that you can access from within any of your components. Here's a link to the new react documentation on context:

Passing Data Deeply with Context

You can also use route parameters as Harsh mentioned. But, I typically reserve route parameters for passing information about how I'm navigating to the desired screen.

答案2

得分: -2

新的React Context文档:useContext,使用Context深层传递数据。
请阅读React Navigation传递参数到路由和初始参数。
如果你使用@react-navigation/bottom-tabs来直接传递props,你可以这样做:

  1. 定义你的屏幕组件:
    import React from 'react';
    import { Text } from 'react-native';
    
    const MyScreen = ({ route }) => {
      const { additionalProp } = route.params;
    
      return <Text>Additional Prop: {additionalProp}</Text>;
    };
    
    export default MyScreen;
  1. 在你的导航配置文件中,使用createBottomTabNavigatorcreateMaterialBottomTabNavigator定义选项卡:
    import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
    import MyScreen from './MyScreen';
    
    const Tab = createBottomTabNavigator();
    
    const MyTabs = () => {
      return (
        <Tab.Navigator>
          <Tab.Screen
            name="MyScreen"
            component={MyScreen}
            initialParams={{ additionalProp: 'Hello, World!' }} // 使用initialParams设置附加属性的初始值
            options={({ route }) => ({
              title: 'My Screen',
              additionalProp: route.params?.additionalProp, // 从route.params中访问附加属性
            })}
          />
        </Tab.Navigator>
      );
    };
    
    export default MyTabs;

在这个例子中,MyScreen组件通过route.params对象接收附加属性,类似于之前的例子。
你可以使用initialParams属性为屏幕设置初始参数。这会设置附加属性的初始值。

要访问附加属性,你可以使用Tab.Screen的options属性,并在options函数中访问route.params?.additionalProproute.params?.additionalProp表达式确保安全地访问additionalProp,即使它不可用。
通过这种方法,你可以在React Navigation v6中将附加属性传递到选项卡内的屏幕,并在屏幕组件内访问它们。

英文:

New documentation for React Context: useContext, Passing Data Deeply with Context.
Please read React Navigation Passing parameters to routes and Initial params
If you use @react-navigation/bottom-tabs for passing props directly you can use this way:

  1. Define your screen component:
    import React from &#39;react&#39;;
    import { Text } from &#39;react-native&#39;;
    
    const MyScreen = ({ route }) =&gt; {
      const { additionalProp } = route.params;
    
      return &lt;Text&gt;Additional Prop: {additionalProp}&lt;/Text&gt;;
    };
    
    export default MyScreen;

  1. In your navigation configuration file, define the tabs using createBottomTabNavigator or createMaterialBottomTabNavigator:
    import { createBottomTabNavigator } from &#39;@react-navigation/bottom-tabs&#39;;
    import MyScreen from &#39;./MyScreen&#39;;
    
    const Tab = createBottomTabNavigator();
    
    const MyTabs = () =&gt; {
      return (
        &lt;Tab.Navigator&gt;
          &lt;Tab.Screen
            name=&quot;MyScreen&quot;
            component={MyScreen}
            initialParams={{ additionalProp: &#39;Hello, World!&#39; }} // Set initialParams with additional props
            options={({ route }) =&gt; ({
              title: &#39;My Screen&#39;,
              additionalProp: route.params?.additionalProp, // Access additional props from route.params
            })}
          /&gt;
        &lt;/Tab.Navigator&gt;
      );
    };
    
    export default MyTabs;

In this example, the MyScreen component receives the additional props via the route.params object, similar to the previous example.
You can set the initial params for the screen using the initialParams property. This sets the initial values of the additional props.

To access the additional props, you can use the options property of Tab.Screen and access route.params?.additionalProp within the options function. The route.params?.additionalProp expression ensures that the additionalProp is accessed safely even if it's not available.
By following this approach, you can pass additional props to screens within tabs in React Navigation v6 and access them within the screen component.

huangapple
  • 本文由 发表于 2023年5月18日 03:34:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定