Spreading properties on DOM elements
There is a common practice that has recently been described as an anti-pattern by Dan Abramov; it also triggers a warning in the console when you do it in your React application.
It is a technique that is widely used in the community, and I have personally seen it multiple times in real-world projects. We usually spread the properties to the elements to avoid writing every single one manually, which is shown as follows:
<Component {...props} />
This works very well, and it gets transpiled into the following code by Babel:
_jsx(Component, props)
However, when we spread properties into a DOM element, we run the risk of adding unknown HTML attributes, which is bad practice.
The problem is not related only to the Spread
operator; passing non-standard properties one by one leads to the same issues and warnings. Since the Spread
operator hides the single properties we are spreading, it is even harder to figure out what...