React Native: TypeError: undefined is not a function (evaluating 'this.props.recipes.map')

React Native: TypeError: undefined is not a function (evaluating 'this.props.recipes.map')

我正在尝试制作一个 React Native 食谱应用程序,您可以在其中将食谱保存在本地,但是当我添加 AsyncStorage 时出现以下错误: Screenshot

这是我的代码:

App.js: https://gist.github.com/anonymous/4e83b8c5ec797278c8a642dfca60c622

Recipes.js: https://gist.github.com/anonymous/c894357b3413f37dbf856c98ef6b93f4

AsyncStorage.getItem 是异步的,所以你的 this.state.recipes 是空的。您应该将 componentWillMount() 更改为

async componentWillMount(){
  let recipes = await AsyncStorage.getItem('recipes);
  if (recipes) {
     this.setState({ recipes });
  }
});

而且你应该确保你的 this.state.recipes 是一个数组。希望对您有所帮助!