简体   繁体   English

无法在 React 中映射数组

[英]Can't map over array in React

I'm passing an array (data) to the Segment component via a prop.我通过 prop 将数组(数据)传递给 Segment 组件。 But then I can't map over data.但是我无法映射数据。 I'm sure it's a really obvious mistake but I just don't see it :(我确定这是一个非常明显的错误,但我只是没有看到:(

import React from 'react';
import SegmentLine from './SegmentLine';

const Segment = ({ data }) => {
  console.log(data);

  return data && data.map((line, i) => <SegmentLine key={i} data={line} />);
};

export default Segment;

The console.log is this: console.log 是这样的:

在此处输入图片说明

Any help is greatly appreciated!任何帮助是极大的赞赏! Thanks in advance :)提前致谢 :)

First, I think you have extra curly braces before the export.首先,我认为您在导出之前有额外的花括号。

Second, you should remove the curly braces between data.map .其次,您应该删除data.map之间的花括号。

return (
  data &&
  data.map((line, i) => {
    return <SegmentLine key={i} data={line} />
  })
)

You can either add a little improvement doing你可以添加一点改进做

return (
  data &&
  data.map((line, i) => (
    <SegmentLine key={i} data={line} />
  ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM