React props: Should I pass the object or its properties? Does it make much difference?

被刻印的时光 ゝ 提交于 2020-01-03 07:04:08

问题


When passing props should I pass the entire object into the child components or should I individually create props first in the parent component and then pass those props to the child?

Pass entire object:

<InnerComponent object={object} />

Individually create props that're needed first:

<InnerComponent name={object.name} image={object.image} />

Which one is preferred and if it depends, what should I use as a gauge to use either?


回答1:


You should prefer your second example, to pass the props individually. There you can see explicitly which arguments are passed and can quickly compare with which arguments are expected. This makes collaboration along people much easier and makes the state of the application more clear.

I personally try to avoid passing whole objects if it's not needed. It's like the spread operator where there can be any property inside and it is much harder to debug in a bigger scope.

Some additions to pure react like prop-types or typescript might help to define clear and expected properties in a big application




回答2:


Since JavaScript has no type annotation, the second choice would be much preferable, as mentioned by @mstruebing the code will be more maintainable. However, if you are using TypeScript React (which support type annotation), both choice would be equally clear. But do keep in mind, the first choice promotes tight coupling between views and models, this allows fast development as you don't have to type much; while the second choice might slows down development but you have higher flexibility and tolerance to future changes.

So, the gauge is: if you favor development speed over flexibility, go for choice 1; if you favor flexibility over development speed, go for choice 2.

Extra notes

Ok, when should we favor development speed over flexibility? Here's my 2 cents.

If your project was meant for short-term usage (for example developing an app just for collecting votes during election), then go for the first choice.

If your project was meant for long-term usage (for example online bank transaction portal), you should go for the second choice.




回答3:


According to the principle of least privilege, this is a correct way:

<InnerComponent name={object.name} image={object.image} />

This restricts InnerComponent from accidentally modifying original object or accessing properties that aren't intended for it.

Alternatively, properties could be picked from original object and passed as props:

<InnerComponent {...pick(object, 'name', 'image')} />

If there are numerous properties that are tedious to list, there may be single prop that accepts an object:

<InnerComponent object={pick(object, 'name', 'image')} />



回答4:


Passing multiple props to a child component is the preferred way of passing props to child components. This will help you in tracking the state of the application in the child component. Also, feel free to use the prop-types module. It acts like generics in Java and provides type safety for the props to pass in.




回答5:


If you aren't making use of all the properties within the object, its better to pass the required properties separately as it can help in performance optimisation when the Child component is a PureComponent of use shouldComponentUpdate. In such a case, only when the props used by the Child component change, its will re-render and not when an unused property from within the object has changed.

However if your are making use of all the properties within the object, it doesn't make much of a difference event if you pass down the object as a props, provided you handle the mutations of the object correctly.




回答6:


It depends on that Object. Do you need ALL of its properties to be passed to another component? Or... You might need to pass 2 properties out of 5 to a specific component?

  • If you don't need to pass ALL of its properties, I recommend passing the Properties as Props
  • BUT, if you are 100% sure that you NEED ALL THE PROPERTIES to be passed together, I recommend passing the whole Object as Props in that case as it would be much clearer code and keeps you away from forgotten to pass something. For example; When you add a new property to that object, you don't have to pass it, as you are already passing the whole Object!

Well, if you aren't certain about your current/future purpose of that Object as you might need to EXCLUDE some properties soon, you probably should pass its properties individually in that case, instead of passing some non-used properties to the other component.



来源:https://stackoverflow.com/questions/52621169/react-props-should-i-pass-the-object-or-its-properties-does-it-make-much-diffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!