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

前端 未结 6 616
执念已碎
执念已碎 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:02

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

    
    

    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:

    
    

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

    
    

提交回复
热议问题