class 组件中的功能组件 React Native

Functional componenet in a class component React Native

您好,我有以下功能组件,它本身可以完美运行

function MeditationList({route, navigation}) {
  const adress = route.params;

  return (
    <View>
      <HeaderBar />
      <Text>
        You have a number of {JSON.stringify(adress.item.numMed)} meditations
      </Text>
    </View>
  );
}

但是当我尝试将其包含在 class 组件中时,它不起作用并出现以下错误

class PreScreen extends Component {
  meditationList = ({route}) => {
    const adress = route.params;

    return (
      <Text>
        You have a number of {JSON.stringify(adress.item.numMed)} meditations
      </Text>
    );
  };
  render() {
    return (
      <ScrollView>
          {this.meditationList()}
      </ScrollView>
    );
  }

我想这是我犯的一些愚蠢的错误。谢谢。

道具不会自动注入到您的方法中。如果你想使用路由,你应该使用 this.props.route

class PreScreen extends Component {
  meditationList = () => {
    const adress = this.props.route.params;

    return (
      <Text>
        You have a number of {JSON.stringify(adress.item.numMed)} meditations
      </Text>
    );
  };
  render() {
    return (
      <ScrollView>
          {this.meditationList()}
      </ScrollView>
    );
  }