Categories

Jul 2, 2019 in React JS

Q:

Why we need to be careful when spreading props on DOM elements?

1 Answer

Jul 2, 2019

When we spread props we run into the risk of adding unknown HTML attributes, which is a bad practice. Instead we can use prop destructuring with ...rest operator, so it will add only required props. For example,

const ComponentA = () =>
  <ComponentB isDisplay={true} className={'componentStyle'} />

const ComponentB = ({ isDisplay, ...domProps }) =>
  <div {...domProps}>{'ComponentB'}</div>
Click here to read more about React JS
Click here to read more about Insurance

Related questions

Madanswer
Jul 2, 2019 in React JS
May 20 in React JS
Oct 3 in React JS
...