我有打字稿代码。单击某些按钮时,必须有一些功能。以componentwillmount为界。

componentWillMount() {
    const { species } = this.props;
    const selectedSpecies =
    species.find(v => v.id == SPECIES_TYPE_CATTLE) || null;
    if (selectedSpecies) {
      this.onSpeciesSelect(selectedSpecies);
     }
}



  TypeError:未定义不是对象(正在评估“ species.find”)

最佳答案

在执行species之前,应检查.find

const selectedSpecies = species ? species.find(v => v.id == SPECIES_TYPE_CATTLE) : null;

07-26 06:45