【问题标题】:react native how to pass props through navigator to another Activity/View反应原生如何通过导航器将道具传递给另一个活动/视图
【发布时间】:2016-08-23 20:06:24
【问题描述】:

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

这是我的 Navigator - 渲染方法代码:

  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});
          }
        }
      } />
    );
  }

这是我传递道具的方式:

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

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

我试过了:

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

这两种方法都不起作用,我知道我可能应该以另一种方式专门通过 renderScene 传递这些道具。可能是通过 createElement 传递路线?

请帮忙。

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

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

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

感谢@stereodenis。

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

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

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

【问题讨论】:

    标签: react-native


    【解决方案1】:

    Navigator 组件的 renderScene 应该是:

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

    【讨论】:

      【解决方案2】:

      passProps 更适合 NavigatorIOS。对于 Navigator,renderScene 将完成这项工作。通常,路由名称是推送的。而 renderScene 将决定声明式 JSX 中的组件和属性。

      推:

      this.props.navigator.push({name: 'Main', gameKey: someKey});
      

      渲染场景:

      renderScene(route, navigator) {
        let component;
        switch(route.name) {
          case 'Main':
            component = <MainActivity title="Main Activity" gameKey={route.gameKey} />;
          default:
            component = null;
        }
        return component;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-24
        • 2020-08-04
        • 1970-01-01
        • 2021-04-13
        • 2018-07-21
        • 1970-01-01
        相关资源
        最近更新 更多