我尝试将一个数组导入到我的父组件中,并将该数据作为道具发送给他的孩子
import Seed from './const/Seed';
export default class ProductList extends React.Component{
render() {
const product = Seed.products[0]
return(
<div>
<Product
id = {product.id}
title = {product.title}
/>
</div>
);
}
}
我收到一个错误:
类型错误:无法读取未定义的属性“0”
也许我没有以正确的方式格式化?
或者有没有办法在不同的文件上声明 const ?
export default class Seed extends React.Component{
render(){
const products = [
{
id: 1,
title: 'Yellow Pail',
},
{
id: 2,
title: 'Green Pail',
}
]
return products
}
}
谢谢
种子文件应该是:
export const products = [
{
id: 1,
title: 'Yellow Pail',
},
{
id: 2,
title: 'Green Pail',
},
]
export default {
products,
}
并将它们导入为(和用法为):
import {products} from './const/Seed';
export default class ProductList extends React.Component{
render() {
const product = products[0]
return(
<div>
<Product
id = {product.id}
title = {product.title}
/>
</div>
);
}
}
render
方法用于在浏览器上呈现内容,而不是用于导出常量。请再次回顾一下react/JavaScript
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)