DEV Community

Discussion on: Do Whatever You Want, So Will I

Collapse
 
maybebored profile image
Mayuran

Please explain why spreading props is a bad idea; is your reasoning is any different from this - codeburst.io/react-anti-pattern-js...

Collapse
 
reergymerej profile image
ᴙɘɘᴙgYmɘᴙɘj

That's a good article and I agree with a lot of it. In general, the problem with spreading props is obscurity and messiness. Child components end up accumulating a lot of stuff that is not meant for them and the origin can become obscured.

Even the React docs state...

Spread attributes can be useful but they also make it easy to pass unnecessary props to components that don’t care about them or to pass invalid HTML attributes to the DOM. We recommend using this syntax sparingly.

Collapse
 
maybebored profile image
Mayuran

Agreed. It would be more precise to say passing unnecessary props to a component is a bad practice. Spreading props is merely a technique allowed by JS. This got me thinking, does React warn when a component receives an unexpected prop, and turns out there is a pluggable solution - github.com/airbnb/prop-types-exact

Collapse
 
kravemir profile image
Miroslav Kravec

The issue isn't just in passing of unnecessary props. That's just a performance micro-optimization, which can be done later,..

Spreading props also allows to hide high amount of tight coupling, and such code looks simple on first sight, and falsely KISS. However, a new developer has to trace and inspect lots of components/layers in order to understand data/control flow of such code.