[英]Loop through an array of strings and render it as a Prop in a Component in React
[英]not able to pass a array as prop to react component and render it
問題:我想將一個數組傳遞給 QuizResult 組件並需要將其顯示在
標簽。
代碼:
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;
我在這里缺少什么?
在這種情況下:
const QuizResult = (result) => {
result
變量是一個 object 包含所有傳遞的道具。 所以你會參考result.result
數組。 相反,解構 function 中的道具:
const QuizResult = ({result}) => {
然后result
將只包含數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.