如何在React Native中使用动态Stack.Screen?

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

How to use dynamic Stack.Screen in React Native?

问题

我在App.js中定义了导航,如下所示:

<NavigationContainer>
  <Stack.Navigator>
    <StackScreenCard
      name="HomeScreen"
      componentName="HomeScreen"
      title="Country City Information"
    />

    <Stack.Screen name="CountriesGrid" component={CountriesGrid} />
  </Stack.Navigator>
</NavigationContainer>

在App.js中使用以下代码导入了StackScreenCard:

import { createNativeStackNavigator } from "@react-navigation/native-stack";
import StackScreenCard from "./components/ui/StackScreenCard";

StackScreenCard.js的代码如下:

import { createNativeStackNavigator } from "@react-navigation/native-stack";

const StackScreenCard = ({ name, componentName, title }) => {
  const Stack = createNativeStackNavigator();

  return (
    <Stack.Screen
      name={name}
      component={componentName}
      options={{
        title: { title },
        headerTintColor: "white",
        headerTitleAlign: "center",
      }}
    />
  );
};

export default StackScreenCard;

它会报错:

错误:导航器只能包含'Screen'、'Group'或'React.Fragment'作为其直接子元素(在屏幕'HomeScreen'中找到'StackScreenCard')。要在导航器中呈现此组件,请将它传递到'Screen'的'component'属性中。

如果我从App.js中移除StackScreenCard的调用,然后屏幕就能正常工作。是否不可能使用动态的Stack.Screen?基本上,我想创建一个类似于单独组件,以便可以在那里编写一次CSS,并为不同的屏幕多次调用该组件。

英文:

I have navigation defined in App.js like below

  &lt;NavigationContainer&gt;
    &lt;Stack.Navigator&gt;
      &lt;StackScreenCard
        name=&quot;HomeScreen&quot;
        componentName=&quot;HomeScreen&quot;
        title=&quot;Country City Information&quot;
      /&gt;

      &lt;Stack.Screen name=&quot;CountriesGrid&quot; component={CountriesGrid} /&gt;
    &lt;/Stack.Navigator&gt;
  &lt;/NavigationContainer&gt;

Imported StackScreenCard using below code in App.js

import { createNativeStackNavigator } from &quot;@react-navigation/native-stack&quot;;
import StackScreenCard from &quot;./components/ui/StackScreenCard&quot;;

StackScreenCard.js code is below

import { createNativeStackNavigator } from &quot;@react-navigation/native-stack&quot;;

const StackScreenCard = ({ name, componentName, title }) =&gt; {
  const Stack = createNativeStackNavigator();

  return (
    &lt;Stack.Screen
      name={name}
      component={componentName}
      options={{
        title: { title },
        headerTintColor: &quot;white&quot;,
        headerTitleAlign: &quot;center&quot;,
      }}
    /&gt;
  );
};

export default StackScreenCard;

It is giving an error message

> Error: A navigator can only contain 'Screen', 'Group' or
> 'React.Fragment' as its direct children (found 'StackScreenCard' for
> the screen 'HomeScreen'). To render this component in the navigator,
> pass it in the 'component' prop to 'Screen'.

If I remove StackScreenCard call from App.js then screens are working fine. Is it not possible to use dynamic Stack.Screen ? Basically I want to create it like a separate component so that I can can write the css once there and call that component multiple times for different screens.

答案1

得分: 1

以下是您要翻译的内容:

  1. The <Stack.Screen /> component needs to directly render inside its parent components which are stack.navigator and navigatorContainer. Therefore make a seperate component and render it inside the screen instead of rendering a component which returns a screen
    中文翻译:<Stack.Screen /> 组件需要直接在其父组件 stack.navigator 和 navigatorContainer 中呈现。因此,创建一个单独的组件,并在屏幕内呈现它,而不是呈现一个返回屏幕的组件。

  2. Instead of making a component and rendering inside the stack.navigator which returns a Stack.Screen component, you can just render a dynamic component and name it inside the screen and by this you can create a separate component and do anything you want be it the styling, pass data etc. You can even make nested navigations inside this component. In this case say e.g Home. Now this Home component can return anything.
    中文翻译:不必创建一个组件并在返回 Stack.Screen 组件的 stack.navigator 内部进行渲染,而是可以直接在屏幕中呈现一个动态组件,并命名它,通过这种方式,您可以创建一个单独的组件,并进行样式设置、传递数据等任何操作。甚至可以在此组件内创建嵌套导航。例如,可以创建一个名为 Home 的组件,而这个 Home 组件可以返回任何内容。

  3. In your App.js
    中文翻译:在您的 App.js 文件中

  4. You can change this screen component as you want
    中文翻译:您可以根据需要更改此屏幕组件

  5. If you want to pass props to this component you can make another component which will return this particular component with props, Let me show you
    中文翻译:如果您想将属性传递给此组件,可以创建另一个组件,该组件将带有属性返回此特定组件,让我为您演示

  6. In App.js
    中文翻译:在 App.js 文件中

  7. Now you can pass the props as you like to this component and change it dynamically. There are other ways also. Feel free to try.
    中文翻译:现在您可以按照您的喜好将属性传递给此组件并进行动态更改。还有其他方法,随时尝试。

英文:

The <Stack.Screen /> component needs to directly render inside its parent components which are stack.navigator and navigatorContainer. Therefore make a seperate component and render it inside the screen instead of rendering a component which returns a screen

Instead of making a component and rendering inside the stack.navigator which returns a Stack.Screen component ,you can just render a dynamic component and name it inside the screen and by this you can create a separate component and do anything you want be it the styling ,pass data etc.You can even make nested navigations inside this component. In this case say e.g Home. Now this Home component can return anything.

in your App.js

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

function App(){
return(
 &lt;NavigationContainer&gt;
        &lt;Stack.Navigator&gt;
          &lt;Stack.Screen name=&quot;ScreenCard&quot; component={ScreenCard}/&gt;
          &lt;Stack.Screen name=&quot;CountriesGrid&quot; component={CountriesGrid} /&gt;
        &lt;/Stack.Navigator&gt;
      &lt;/NavigationContainer&gt;
)

}

<!-- end snippet -->

In your Component,ScreenCard

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import {View,Text} from react Native
const ScreenCard = () =&gt; {
  
  return (
    &lt;View&gt; 
       &lt;Text&gt; Screen card &lt;/Text&gt;
   &lt;/View&gt;
  );
};

export default StackScreenCard;

<!-- end snippet -->

You can change this screen component as you want

If you want to pass props to this component you can make another component which will return this particular component with props, Let me show you

In App.js

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    function App(){

    function StackScreenCard(){
    return &lt;ScreenCard data={dataExample} color={colorExample} /&gt;
    }

    return(
     &lt;NavigationContainer&gt;
            &lt;Stack.Navigator&gt;
              &lt;Stack.Screen name=&quot;ScreenCard&quot; component ={StackScreenCard}/&gt;
              &lt;Stack.Screen name=&quot;CountriesGrid&quot; component={CountriesGrid} /&gt;
            &lt;/Stack.Navigator&gt;
          &lt;/NavigationContainer&gt;
    )

    }

<!-- end snippet -->

Now you can pass the props as you like to this component and change it dynamically. There are other ways also. Feel free to try.

huangapple
  • 本文由 发表于 2023年3月8日 15:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670461.html
匿名

发表评论

匿名网友

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

确定