首页 > 解决方案 > 将数组传递给 React 组件,然后在子组件中访问该数组中的值

问题描述

使用 React(但我认为问题是 Javascript),我试图将数组传递给 React 组件,然后在子组件(消耗数组)中 - 从数组中取出值。

我想要做的是访问我的数组中的值:“_label”。我试过做各种版本的东西,比如:key._label但没有成功!

家长:

      <StyledHorizontalAttributes>
        {objects[0].attributeCollection.questions.map((question) => (
          <FirstRepeatAttributeLabelAssistant key={question.key} />
        ))}
        {console.log(objects[0].attributeCollection.questions)} // returns [StringAttributeModel (below)]
      </StyledHorizontalAttributes>

孩子:

const FirstRepeatAttributeLabelAssistant = ({ label, assistant, key }) => {
  return (
    <StyledGroup
      as={Row}
    >
      <StyledLabelWrapper>label</StyledLabelWrapper>
      {/* {isGroupedInput && ( */}
      <Column
        size={12}
      />

      <Column>
        <StyledAssistantWrapper>assistant</StyledAssistantWrapper>
      </Column>
    </StyledGroup>
  );
};


大批:

在此处输入图像描述

标签: javascriptarraysreactjs

解决方案


于忘记传递label给子组件:

<FirstRepeatAttributeLabelAssistant key={question.key} label={question._label} />

{}用于从中获取价值label

<StyledLabelWrapper>{label}</StyledLabelWrapper>

推荐阅读