首页 > 解决方案 > React Native 在两个屏幕中的两个类组件之间传递数据

问题描述

  1. 我有screen1和screen2。在 screen1 中,我有一个类组件 A。在 screen2 中,我有一个类组件 B。在 A 中,我有状态“分数”,我想将其传递给 screen2 中的 B。

  2. 我试过了:

在屏幕 1 中:

this.props.navigation.navigate('screen2', {
   score: [this.state.score],
});

在屏幕 2 中:

const data = this.props.navigation.getParam('score');

这没用。我认为这是因为 react-navigation 已更新。

  1. 我查找了https://reactnavigation.org/docs/params/,但示例是函数,而不是类组件。

太感谢了!

标签: reactjsreact-native

解决方案


在较新的版本中,react-navigation您可以访问传递给screen2类组件的道具,如下所示:

this.props.route.params.score


推荐阅读