reactjs - 无法读取 React 应用程序中未定义的图像
问题描述
我使用 filter 方法在数组中挑选出一个特色项目,并使用它在我的 Home 组件中呈现它的状态。我在 MainComponent 中使用 filter 方法,将其传递给 Home 组件,我希望 AddCard 功能组件在其中呈现。我收到“无法读取未定义的图像”的错误消息,所以我假设我在传递道具时做错了。
MainComponent.js
class Main extends Component{
constructor(props){
super(props);
this.state={
whyfastpack: WHYFASTPACK
};
}
render(){
const LandingPage =()=>{
return (
<Landing />
);
};
const HomePage=()=>{
return (
<Home
lightweight={this.state.whyfastpack.filter(lightweight => lightweight.featured)[0]}
/>
);
};
return(
<div>
<Header />
<Switch>
<Route exact path='/' component={LandingPage} />
<Route path ='/home' component={HomePage} />
HomeComponent.js
...
</div>
<div className='col-md m-1'>
<AddCard />
</div>
</div>
....
function RenderCard({ item }) {
return (
<Card>
<CardImg src={this.image} alt={item.name} />
<CardBody>
<CardTitle>{item.name}</CardTitle>
<CardText>{item.comment}</CardText>
</CardBody>
</Card>
);
}
function AddCard(props) {
return (
<div className="container">
<div className="row">
<div className="col-md m-1">
<RenderCard item={props.lightweight} />
</div>
</div>
</div>
);
}
为什么FastPack.js
export const WHYFASTPACK = [
{
id: 0,
image: 'images/pnw.jpg',
name: 'Reduce Weight',
comment: 'Carry only what you need for the day, not a month! With fast-packing you only need to bring enough to get you through each day!',
featured: true,
}
]
解决方案
您忽略了将lightweight
道具传递给AddCard
from HomeComponent
。
<div className='col-md m-1'>
<AddCard lightweight={this.props.lightweight} />
</div>
推荐阅读
- angular5 - 使用 post() 自动传递保存在 cookie 中的会话
- php - Htaccess 多域,但重定向一个域,除了特定路径
- c# - Xamrin.Android 蓝牙配对
- google-sheets - 从单列和填充列中查询值,均基于多个条件
- java - 来自数据库的未经授权的错误 oauth2 客户端
- .net - 在安装期间确定 .NET Core 版本(IzPack、BitRock、Install4J)
- c# - 如何将工作簿A中的Excel工作表复制到工作簿B
- java - Eclipse e4 - 缺少约束:需求能力:osgi.extender
- c# - 在解决方案 .NET 中存储可用于不同项目的文件
- python - 使用 smtplib 服务器接受带空格的电子邮件