A
A
Alex Mirgorodskiy2020-11-10 15:31:11
React
Alex Mirgorodskiy, 2020-11-10 15:31:11

How to reduce the transfer of props to the component?

Hello everyone, I'm just getting started with React. A fan of code refactoring. Could you tell me how to shorten such a record?

function MyComponentItem({items}) {
      return (...)
   }
   function MyComponent({items}) {
       return (<div> 
         { <MyComponentItem items={items}  /> }
       </div>)
   }


Specifically, I don't like it.
<MyComponentItem items={items} />

It can be like that,
<MyComponentItem {items} />
or
<MyComponentItem {{items}} />
well, or something similar in the spirit of destructuring. Could you tell?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Mirgorodskiy, 2020-11-10
@AlexWeb6667

As a result, I came to this, it is convenient if there are more than one props. But as I understand it, an extra operation occurs, the creation of an object and its destructuring, I didn’t test how this affects performance

function MyComponentItem({items, settings, itemWidth}) {}

    <MyComponentItem {...{items, settings, itemWidth}}  />

K
Kirillsp1982, 2020-11-11
@Kirillsp1982

Hey! Try this, it should work: <MyComponentItem {...items} />
PS By the way, this will pass all the elements of the items object from the prop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question