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

前端 未结 6 619
执念已碎
执念已碎 2020-12-15 17:31

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

6条回答
  •  眼角桃花
    2020-12-15 18:06

    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.

提交回复
热议问题