0

I'm passing an array (data) to the Segment component via a prop. 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:

enter image description here

Any help is greatly appreciated! Thanks in advance :)

4
  • 2
    remove {} around map, also it seems like your data from some api calls, so you need to make your code to wait till the data arrives then only you can perform map Commented Mar 31, 2020 at 15:30
  • @CodeManiac Thank you so much!!! I can't believe this was the mistake... Did I mention I have worked with react for a few months already :'D Commented Mar 31, 2020 at 15:32
  • The waiting is fixed through the data && I think Commented Mar 31, 2020 at 15:33
  • 1
    No problem sometime all it needs is other person eyes :) No && doesn't fix waiting Commented Mar 31, 2020 at 15:34

2 Answers 2

2

First, I think you have extra curly braces before the export.

Second, you should remove the curly braces between data.map.

return (
  data &&
  data.map((line, i) => {
    return <SegmentLine key={i} data={line} />
  })
)
Sign up to request clarification or add additional context in comments.

Comments

1

You can either add a little improvement doing

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.