反应原生如何通过导航器将道具传递给另一个活动/视图 [英] react native how to pass props through navigator to another Activity/View

查看:45
本文介绍了反应原生如何通过导航器将道具传递给另一个活动/视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的解决方案(因为它对我来说很直观),但我找不到任何可行的解决方案.

It seems like simple solution (as it was intuitively for me), but I couldn't find any working solution.

这是我的导航器 - 渲染方法代码:

Here is my Navigator - render method code:

  render() {
    return (
      <Navigator initialRoute={{title:'Games', component:Games}}
      configureScene={() => {
                    return Navigator.SceneConfigs.FloatFromRight;
                }}
      renderScene={(route, navigator) =>
        {
          if (route.component) {
            return React.createElement(route.component, {navigator});
          }
        }
      } />
    );
  }

这是我传递道具的方式:

Here is how I'm passing props:

this.props.navigator.push({title: 'MainActivity', component:MainActivity, gameKey:key, passProps:{title:'bla'}});

这是我想要获取这些道具的 MainActivity.

Here is a MainActivity where I want to get those props.

我试过了:

this.props.route.gameKey, 
this.route.gameKey,
this.props.gameKey

都没有奏效,我知道我可能应该以另一种方式专门通过 renderScene 传递这些道具.可能是通过 createElement 传递路由?

neither worked, I understand that may be I should pass those props specifically through renderScene, the other way. May be passing route through createElement?

请帮忙.

问题更严重,因为我已经通过 Navigator 将道具从一个活动传递到另一个活动.

The problem was more serious, because I've passed props from one activity to the other via Navigator.

因此,每次您需要的任何东西(从任何活动到任何活动)都在使用时传递的解决方案

So, the solution to pass every time anything U need (from any activity to any) is using in

return React.createElement(route.component, {navigator, route});

感谢@stereodenis.

Thanks to @stereodenis.

在任何活动中这样推送后:this.props.navigator.push({title: 'MainActivity', component:MainActivity, gameKey:key, passProps:{title:'bla'}});

After push like this in any activity: this.props.navigator.push({title: 'MainActivity', component:MainActivity, gameKey:key, passProps:{title:'bla'}});

然后通过以下方式访问它们:this.props.route.gameKey

And then access them via: this.props.route.gameKey

为了完全清楚 - 现在可以访问路由,因为每次推送时都会在 createElement 中传递 {navigator, route},所以这是另一个道具.

To make it totally clear - route is now accessible, because of passed {navigator, route} in createElement every time U push, so this is yet another prop.

推荐答案

Navigator组件的renderScene应该是:

The renderScene of the Navigator component should be:

{ title, gameKey, passProps } = route;
return React.createElement(route.component, {
  navigator,
  title,
  gameKey, 
  passProps
})

这篇关于反应原生如何通过导航器将道具传递给另一个活动/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆