[英]not able to pass a array as prop to react component and render it
Problem: I want to pass a array to QuizResult component and need to display it in问题:我想将一个数组传递给 QuizResult 组件并需要将其显示在
tag.标签。
Code:代码:
markedAnswers = [0,1,2,3,1,1,4,4]
<QuizResult result={markedAnswers}/>
const QuizResult = (result) => {
return (
<div className="result-screen">
{result.map((data, index) => <p key={index}> {data}</p>)} - result.map is not a function error
<div>{result}</div> - no error, empty div is shown
</div>
);
};
export default QuizResult;
What i am missing here?我在这里缺少什么?
In this case:在这种情况下:
const QuizResult = (result) => {
The result
variable is an object containing all of the passed props. result
变量是一个 object 包含所有传递的道具。 So you'd refer to result.result
for the array.所以你会参考
result.result
数组。 Instead, destructure the props in the function:相反,解构 function 中的道具:
const QuizResult = ({result}) => {
Then result
would contain just the array.然后
result
将只包含数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.