Spreading props 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, as follows:
<Component {...props} />
This works very well and it gets transpiled into the following code by Babel:
React.createElement(Component, props);
However, when we spread props into a DOM element, we run the risk of adding unknown HTML attributes, which is a 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 makes even harder to figure out what we are passing to the element.
To see...