React's props with the same name as their value

后端 未结 4 1201
清酒与你
清酒与你 2020-12-25 11:19

Can we pass props to a component that has the same name and value implicitly?

Example: Let\'s say that I have a variable called x: const x = 1

4条回答
  •  不知归路
    2020-12-25 12:01

    Booleans can be passed implicitly to the component as @Ajay also pointed out in comments, like

    which is basically equivalent to

    However, if your variable is something other than a boolean, than you need to write it like

    Or if you have a number of such props, you can form an object like

    const cmpProps = {
       x,
       y,
       foo,
       bar
    }
    

    and pass them using Spread attributes like

    
    

提交回复
热议问题