Answer the question
In order to leave comments, you need to log in
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>)
}
<MyComponentItem items={items} />
<MyComponentItem {items} />
<MyComponentItem {{items}} />
Answer the question
In order to leave comments, you need to log in
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}} />
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 questionAsk a Question
731 491 924 answers to any question