【问题标题】:send data from screen to Tab screen using react-native-router-flux使用 react-native-router-flux 将数据从屏幕发送到选项卡屏幕
【发布时间】:2019-03-20 09:46:09
【问题描述】:

我正在尝试使用 react-native-router-flux 将数据从一个屏幕发送到另一个屏幕,但问题是我无法在屏幕的接收端接收数据。这是我的代码 sn-p:

在 router.js 中

<Router>
    <Stack key="root">
      <Scene
        key="signup"
        component={Signup}
        title=""
        initial={true}
        hideNavBar={true}
      />
      <Scene key="login" component={Login} title="" />

      <Scene key="bottomtourbar" tabs={true} hideNavBar={true}>
        <Scene
          key="toursHome"
          component={Tours}
          title="Tours"
          hideTabBar={true}
          hideNavBar={false}
        />
        <Scene
          key="analytics"
          component={Analytics}
          title="Analytics"
          hideNavBar={true}
          hideNavBar={false}
        />
       </Scene>
      </Stack>
  </Router>

在 Login.js 中,我使用下面的代码将参数传递给 toursHome 页面

Actions.toursHome({dataResponse:jsonResponse});

在 ToursHome.js 文件中我试图访问登录页面发送的参数

export default class Tours extends Component {
  constructor(props) {
    super(props);
  };
  render() {
    const responseData = this.props.dataResponse;
    console.log(responseData);
    return (
      <View style={styles.container}>
        <ToursTab />
        <View style={styles.TabbedContainer}>
          <Text>{responseData}</Text>
        </View>

        <ToursBottom />
      </View>
    );
  }
}

在控制台中它打印为未定义。

通过使用“Actions.login({dataResponse:jsonResponse});”从注册屏幕发送道具到登录有效,但是从登录到标签栏屏幕发送道具失败。 谁能帮我解决这个问题。

【问题讨论】:

  • "jsonResponse" 有一些数据或者你试图添加简单的字符串??
  • jsonResponse 由 json 数组组成,我在发送之前打印了 jsonResponse ,它显示数据存在。

标签: javascript react-native react-native-android react-native-ios react-native-router-flux


【解决方案1】:

我已经尝试以多种不同的方式实现这一目标。但是我学到了“艰难的方式”,通过Actions 将道具传递到不同级别的场景(深度,在你的情况下toursHome 场景嵌套在bottomtourbar 场景下)只能通过 redux 正确完成。如果您的场景已连接到 redux(使用 connect()),则更新 store 中的状态,它应该可以通过 mapStateToProps 提供给您连接的组件。

否则,使用Actions 在场景之间传递道具对于相同级别/深度的场景是可行的(例如,在您的情况下,它将在analyticstoursHome 之间成功传递Actions 的道具或signuplogin)

【讨论】:

  • 感谢您的回复,找不到任何其他方式,因此使用 AsyncStorage 存储数据以获取数据。
  • 可以给sn-p吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 2019-01-23
相关资源
最近更新 更多