[英]Map on props of ReactJS Redux
I started my learning path few months ago (html, css, js) and I have a question for an issue that I have with react (just started learning it).几个月前我开始了我的学习路径(html,css,js),我有一个关于反应的问题(刚开始学习)。
I have an error that says: data.map is not a function我有一个错误说: data.map 不是 function
I want to loop trough my array of objects with map, and dispatch the props (title, answer) to the child for each loop, to make a list of different FaqComponent having each the {title and answer}我想用 map 循环遍历我的对象数组,并为每个循环将道具(标题,答案)分派给孩子,以列出不同的 FaqComponent,每个都有 {title 和 answer}
const data = useSelector(state => ({
...state.homePage.list
}))
console.log(data);
return (
<div>
{data.map((title, answer) => (
<FaqComponent
title={title}
answer={answer}
/>
))}
</div>
);
}
export default ...;
Thanks for your replies感谢您的回复
You're using an object {}
instead of an array []
syntax.您使用的是 object
{}
而不是数组[]
语法。
Try with:尝试:
const data = useSelector(state => ([
...state.homePage.list
]));
You should declare "data"s type, if it is an array so your function should be like:你应该声明“数据”的类型,如果它是一个数组,那么你的 function 应该是这样的:
const data = useSelector(state => ([
...state.homePage.list
]))
console.log(data);
return (
<div>
{(data && data.length > 0) ? data.map((item, i) => (
<FaqComponent
title={item.title}
answer={item.answer}
key={i}
/>))
: <div>No Result!...</div>}
</div>
);
}
export default ...;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.