简体   繁体   English

无法将数组作为道具传递给反应组件并渲染它

[英]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.

相关问题 循环遍历字符串数组并将其呈现为 React 组件中的 Prop - Loop through an array of strings and render it as a Prop in a Component in React 无法在反应中将数组传递给另一个组件 - Not able to pass Array into another component in react 如何将 function(在数组内)作为 prop 传递给 React 中的子组件 - How to pass a function (within an array) as a prop to a sub component in React React传递prop到其他组件以从状态数组中删除 - React pass prop to other component to delete from state array 反应组件中的错误传递道具 - Wrong pass prop in react component 如何将React组件作为prop传递 - How to pass a React component as a prop 如何将自定义组件作为“属性”传递给React.js渲染函数中的另一个自定义组件? - How to pass a custom component as a 'prop' to another custom component in React.js render function? 传入样式表作为功能组件中渲染的道具 - Pass in a stylesheet as a prop for a render in a functional component 如何将组件作为道具传递并有条件地渲染它? - How to pass component as a prop and render it conditionally? 带有“as”属性的通用 React TypeScript 组件(能够呈现任何有效的 dom 节点) - Generic React TypeScript component with 'as' prop (able to render any valid dom node)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM