React's props with the same name as their value

后端 未结 4 1200
清酒与你
清酒与你 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 11:54

    I had a slight epiphany when reading these answers. Since in ES6+ you can add an existing variable to an object like this:

    const x = 42;
    const obj = { x, y: 1337 };
    console.log(obj); // result { x: 42, y: 1337 }
    

    That means you can add a named prop to a React component like:

    const x = 42;
    const elm = ;
    

提交回复
热议问题