[英]props.map is not a functions react.js
been working on react js project.一直在做 react js 项目。 But while passing props to child component getting error
props.map
is not a function.但是在将道具传递给子组件时得到错误道具
props.map
不是 function。
ParentComponent:-父组件:-
componentDidMount = () => {
fetch('api/dashboard/dashboarddata').then(response => response.json()).then((data: Dashboard) => {
this.setState({
category: data.categories,
banners: data.banners,
loading: false
});
});
}
Passing Props传递道具
{
this.state.loading ? <ContentLoader /> : <div><Category {...this.state.category} /> <Banner {...this.state.banners} /> </div>
}
This is what state object after API response:-这是 API 响应后的 state object 响应:-
But in child component getting error但是在子组件中出现错误
Child Component:子组件:
const Category = (props: brandcategory[]) => {
return (
<div className="section category">
<div className="container-fluid">
<div className="row">
<div className="col-md-12">
<div className="section-title">
<h4 className="title">Select interested categories</h4>
<div className="section-nav">
<a className="primary-btn cta-btn" href="#">View All</a>
</div>
</div>
</div>
<div className="col-md-12">
<div className="row">
<div className="col-md-10">
<div className="products-tabs">
<Slider {...sliderconfig.productSlider} className="products-slick" data-nav="#slick-nav-2">
{
props.map((c: brandcategory) => <CategoryCard key={c.id} {...c} />)
}
</Slider>
</div>
</div>
<div className="col-md-2 text-center">
<div className="btn-container">
<a href="#" className="btn btn-3">Get Started</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
)
}
Receiving props in child component like this:-像这样在子组件中接收道具:-
The props
is an object, you likely meant to map over a specific prop you passed, like props.category.map
. props
是 object,您可能意味着 map 通过您传递的特定道具,例如props.category.map
。 You are actually attempting to spread the entire this.state.category
array into props.您实际上是在尝试将整个
this.state.category
数组传播到道具中。
Pass this.state.category
array as a prop. this.state.category
数组作为道具传递。
<Category category={this.state.category} />
Then in Category
component access props.category
for mapping.然后在
Category
组件中访问props.category
进行映射。
props.category.map((c: brandcategory) => <CategoryCard key={c.id} {...c} />)
I'm not as familiar with Typescript as I could/should be, but I'm sure you will need to update the component signature.我可能/应该对 Typescript 不太熟悉,但我确信您需要更新组件签名。 Perhaps something like the following:
也许类似于以下内容:
const Category = ({ category: brandcategory[] }) => { ....
And in this case category
is already destructured from the props
object, so you won't need to access as props.category.map
but rather just category.map
.在这种情况下,
category
已经从props
object 解构,因此您不需要访问props.category.map
而只是category.map
。
category.map((c: brandcategory) => <CategoryCard key={c.id} {...c} />)
You have provided array with spread operators to react component, which must be object as key, value pair must exist.您提供了带有扩展运算符的数组来反应组件,它必须是 object 作为键,值对必须存在。 Refer below examples:-
请参阅以下示例:-
const Category = ({categories}) => { return ( <ul> {categories.map((category, index) => ( <li key={index}>{category.name}</li> ))} </ul> ); } const App = () => { const categories = [{name: 'Apple'}, {name: 'Ball'}]; return <Category categories={categories} />; } const App2 = () => { const catProps = {categories: [{name: 'Apple 2'}, {name: 'Ball 2'}]}; return <Category {...catProps} />; } ReactDOM.render( <App/>, document.getElementById('root') ); ReactDOM.render( <App2/>, document.getElementById('root2') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <h1>Example 1</h1> <div id="root"></div> <h1>Example 2</h1> <div id="root2"></div>
This error occurs because your data is not an array.发生此错误是因为您的数据不是数组。 So.map() function only works with arrays.
So.map() function 仅适用于 arrays。 First, you will need to confirm your data type.
首先,您需要确认您的数据类型。 and also you will need to transform your data into an array.
而且您还需要将数据转换为数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.