类组件中的功能组件React Native。

问题描述 投票:0回答:1

嗨,我有以下功能组件,它本身就能完美地工作。

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

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

reactjs react-native react-native-navigation
1个回答
1
投票

如果你想使用路由,你应该使用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>
    );
  }
© www.soinside.com 2019 - 2024. All rights reserved.