0
import React from 'react';
import { Map } from 'react-feather';

const details = [
  {
    title: 'location',
    icon: <Map />,
    meta: 'foo',
  },
  { title: 'type', icon: <Map />, meta: 'type' },
  { title: 'duration', icon: <Map />, meta: 'duration' },
  { title: 'date', icon: <Map />, meta: 'date' },
  { title: 'time', icon: <Map />, meta: 'time' },
];

const DetailList = () => {
  return (
    <ul>
      {details.map(item => (
        <li>
          <div>
            <div>{item.icon}</div>
            <div>{item.meta}</div>
          </div>
        </li>
      ))}
    </ul>
  );
};

export default DetailList;

Objective: To map an array and display icon along with other information. However I bumped into error:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

I don't think there is any problem with the import from react-feather. Because I'm able to use it with <div><Map /></div> but not in mapping and render from the array.

Does anyone know what is my problem? Or am I missing anything?

2
  • What specific line is it complaining about? Also, do you happen to be using webpack? Looking at the react-feather package info I noticed it warns that with webpack you have to use imports like import Map from 'react-feather/dist/icons/map' instead. Commented Apr 13, 2018 at 4:49
  • The above code seems correct. However, why are you returning a <div> in a <li> Commented Jul 13, 2021 at 4:20

2 Answers 2

2

You probably want something like this:

import React from 'react';
import { Map } from 'react-feather';

const details = [
  {
    title: 'location',
    icon: Map,
    meta: 'foo',
  },
  { title: 'type', icon: Map, meta: 'type' },
  { title: 'duration', icon: Map, meta: 'duration' },
  { title: 'date', icon: Map, meta: 'date' },
  { title: 'time', icon: Map, meta: 'time' },
];

const DetailList = () => {
  return (
    <ul>
      {details.map(({ icon, meta }) => (
        <li>
          <div>
            <div><icon /></div>
            <div>{meta}</div>
          </div>
        </li>
      ))}
    </ul>
  );
};

export default DetailList;

I'm not sure that JSX handles passing of initialized components particularly well.

Sign up to request clarification or add additional context in comments.

1 Comment

You might wanna mention that this is probably not a good solution. Also, nice name ;)
0

I don't think there is any problem with [...]

Please make sure before posting a question. Check if you have Map in scope. Your component should work.

btw: in component like this you can map your array of objects to array of React elements once, not on every render of DetailList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.