C
C
cester2018-04-02 17:10:53
JavaScript
cester, 2018-04-02 17:10:53

How to make render without div, react?

Good afternoon! Who knows how to render without using react in this case div?
In react 16 there is a Fragment for this, but now I have a lower version.
Who knows how to do it?

const Item= ({ list}) => (
  <div>
    {list.map(li=> (
      <p key={li}>{li} </p>
    ))}
  </div>
);

In this case
const Item = ({ list}) => list.map(li=> <p key={li}> {li}</p>);

mistake
A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2018-04-02
@miraage

const Item = ({ list }) => (
  <React.Fragment>
    {list.map(li => <p key={li}>{li}</p>)}
  </React.Fragment>
);

// EDIT
const Fragment = ({ children }) => children;

const Item = ({ list }) => (
  <Fragment>
    {list.map(li => <p key={li}>{li}</p>)}
  </Fragment>
);

G
genius_spirit, 2018-04-05
@genius_spirit

Fragment is optional, just like div. just need to return one main container, it can be or or etc.
your error says that in map you need to do return:
const Item = ({ list}) => list.map(item => return { item });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question