reactjs - 获取“TypeError:无法读取未定义的属性'map'”
问题描述
当我尝试运行我的代码时,它显示此错误:无法读取未定义的属性“地图”
这是我的代码:
import React, { Component } from 'react';
import Recipe from './Recipe';
import RecipeSearch from './RecipeSearch';
export default class RecipeList extends Component {
render() {
const { recipes } = this.props;
return (
<React.Fragment>
<RecipeSearch />
<div className="container my-5" >
{/*title*/ }
<div className="row">
<div className="col-10 mx-auto col-md-6 text-center text-uppercase mb-3">
<h1 className="text-slanted">recipe list</h1>
</div>
</div>
{/*end of title*/}
<div className="row">
{recipes.map(recipe =>{
return <Recipe key={recipe.recipe_id}
recipe={recipe}/>;
})}
</div>
</div>
</React.Fragment>
);
}
}
那么我该如何解决呢?
解决方案
问题是由于您的recipes
道具是undefined
.
你应该经常检查你是否在你的props
,
{recipes && recipes.length > 0 && recipes.map(recipe =>{
return <Recipe key={recipe.recipe_id} recipe={recipe}/>;
})}
推荐阅读
- autodesk-forge - Inventor 装配体未在 Forge Viewer 中正确显示外观
- c++ - 在以下 cpp 代码中出现 SIGESV 错误
- php - Mongodb php datetime过滤器无法正常工作
- ubuntu - 上传的包未显示在我的 Launchpad PPA 中
- c# - 是否可以将没有参数的方法分配给 Func
在参数中 - reactjs - React-Redux - 通过 ID 获取用户
- actions-on-google - 无法显示列表选择
- python-3.x - 意外的空白窗口 Tkinter
- clojure - Clojure ring-cors/wrap-cors 设置
- gnuplot - 在 gnuplot 上拟合指数函数