Skip to content

Commit

Permalink
Spreading props on DOM elements
Browse files Browse the repository at this point in the history
  • Loading branch information
vasanthk committed Mar 26, 2017
1 parent 8f2fd3d commit 6c4cd36
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions anti-patterns/07.spreading-props-dom.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Spreading props on DOM elements
* When we spread props we run into the risk of adding unknown HTML attributes, which is a bad practice.
*
*
* @Reference:
* React Design Patterns and best practices by Michele Bertoli
*/

// BAD
// This will try to add the unknown HTML attribute `flag` to the DOM element

const Sample = () => (<Spread flag={true} className="content"/>);
const Spread = (props) => (<div {...props}>Test</div>);

// GOOD
// By creating props specifically for DOM attribute, we can safely spread.
const Sample = () => (<Spread flag={true} domProps={{className: "content"}}/>);
const Spread = (props) => (<div {...props.domProps}>Test</div>);

0 comments on commit 6c4cd36

Please sign in to comment.