简体   繁体   English

将 props 发送到 @react-navigation/drawer 组件

[英]sending props to @react-navigation/drawer component

I am trying to send prop to component inside Drawer.Screen.我正在尝试将道具发送到 Drawer.Screen 内的组件。 I am using @react-navigation/drawer.我正在使用@react-navigation/drawer。 I finally send props to navigation container but I cant send props to component={homeStackScreen} in Drawer.Screen.How can I send props to homeStackScreen class. And it is not written in documentation.我终于将道具发送到导航容器,但我无法将道具发送到 Drawer.Screen 中的 component={homeStackScreen}。如何将道具发送到 homeStackScreen class。它没有写在文档中。 need help please.请需要帮助。

const Drawer = createDrawerNavigator();

<NavigationContainer>
            <Drawer.Navigator initialRouteName="Home">
                {props.leftMenu.map((x,index) =>                 
                <Drawer.Screen name={x.name} key = {index} component={homeStackScreen} />
                )}
            </Drawer.Navigator>
</NavigationContainer>

Im using this aproche to pass "font" prop:我使用这个 aproche 来传递“字体”道具:

in App.js在 App.js 中

 <NavigationContainer>
    <Drawer.Navigator>
      <Drawer.Screen
        name="Home"
        component={HomeScreen}
        initialParams={{ font: TajawalFontFamily }}
      />
    </Drawer.Navigator>
  </NavigationContainer>

In HomeScreen.js在 HomeScreen.js 中

export default function HomeScreen({ route }) {
const font = route.params.font;
...

Here is how you can pass the props to the components from Drawer.Screen easily by replacing the simple component from component={} and replacing it with an anonymous function which will return our component where we need to navigate.以下是如何通过从component={}替换简单组件并将其替换为匿名 function 来轻松地将道具从Drawer.Screen传递给组件,这将返回我们需要导航的组件。

Usually, we declare a screen like this:通常,我们这样声明一个屏幕:

<Drawer.Screen name="Home" component={HomeScreen}/>

to pass the props to the component, we will just make the following modification with the help of the anonymous function and pass the props.要将 props 传递给组件,我们将在匿名 function 的帮助下进行以下修改并传递 props。

Like this:像这样:

<Drawer.Screen name="Home" component={() => <HomeScreen data={"your_data"}/>}/>

Simple Example:简单示例:


    return (
        <NavigationContainer>
            <Drawer.Navigator initialRouteName="Home">
                <Drawer.Screen name="Home" component={() => <HomeScreen data={"your_data"}/>}/>
                <Drawer.Screen name="Notifications" component={NotificationsScreen} />
            </Drawer.Navigator>
        </NavigationContainer>
    );

}}

//.............

function HomeScreen({data}) {
    return (
            <View>
            <Text>{data}</Text>
            </View>
    );
}

In your case, it should look something like code given below:在您的情况下,它应该类似于下面给出的代码:

const Drawer = createDrawerNavigator();

<NavigationContainer>
            <Drawer.Navigator initialRouteName="Home">
                {props.leftMenu.map((x,index) =>                 
                <Drawer.Screen name={x.name} key = {index} component={() => <homeStackScreen data={x}/>} />
                )}
            </Drawer.Navigator>
</NavigationContainer>

Similar question for more: Passing state array from class into function with pure react native更多类似问题: Passing state array from class into function with pure react native

There is a props named initialParams in Drawer.Screen given in the docs of React navigation 5.x to pass a props to component.在 React navigation 5.x 的文档中给出的 Drawer.Screen 中有一个名为initialParams的道具,用于将道具传递给组件。 If you navigate to a new screen, the params passed are shallow merged with the initial params.如果您导航到一个新屏幕,传递的参数将与初始参数浅层合并。

Check here: https://reactnavigation.org/docs/5.x/screen#initialparams在这里查看: https://reactnavigation.org/docs/5.x/screen#initialparams

Use it like this:像这样使用它:

<Drawer.Screen name="Home" component={HomeScreen} initialParams={{data:"your props"}}/>

The accepted answer is correct, but it will result in this warning: Passing an inline function will cause the component state to be lost on re-render.接受的答案是正确的,但会导致此警告:Passing an inline function will cause the component state to be lost on re-render。 This would be a better way of passing props to the component:这是将道具传递给组件的更好方法:

<Drawer.Screen
    name={'Home'}
>
    {(props) => (
        <HomeScreen
            yourProps={"yourProps"}
        />
    )}
</Drawer.Screen>

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

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