Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a best practice to only supply props shallowly in React components?

Tags:

reactjs

I've noticed that React only freezes the props shallowly, and that React.cloneElement only merges in props shallowly. So should we only be passing in props shallowly when we can?

like image 811
Hel Avatar asked Sep 16 '25 17:09

Hel


1 Answers

You can definitely pass objects in props, I don't think it is a bad practice at all, although it's a good idea to define PropTypes on your component so that its users know what to pass in.

I'd also suggest looking into making your model immutable. Immutable objects are always 'frozen', plus they have other benefits, like allowing you to implement shouldComponentUpdate in a very efficient way, do deep comparison / deep cloning trivially etc. This kind of thing plays really well with react.

like image 143
Vladimir Rovensky Avatar answered Sep 18 '25 09:09

Vladimir Rovensky