Using React.clone element vs not using it for applying a prop

363 Views Asked by At

i saw a code snippet which write this:

<OmniCarousel settings={sliderSettings}>
        {React.Children.map(children, (child, index) => <div key={`dashboardBanner_${index}`}>{child}</div>)}
      </OmniCarousel>

but i was wondering why it was not written as:

<OmniCarousel settings={sliderSettings}>
            {React.Children.map(children, (child, index) => React.cloneElement(child,<div key={`dashboardBanner_${index}`}>{child}</div>)}

The code compiles fine both ways. Is there someone to answer this?

1

There are 1 best solutions below

0
On BEST ANSWER

They both are almost the same, with only difference in references. If you want to pass ref to child you can use React.cloneElement.

Docs: https://reactjs.org/docs/react-api.html#cloneelement