简体   繁体   English

反应解构 state 传递到组件道具

[英]React destructuring state to pass into component props

I am passing in part of my state into component props like this:我将我的 state 的一部分传递给这样的组件道具:

<EditBoard
  boardArr={{
    boardid: this.state.boardid,
    boardvalue: this.state.boardvalue,
    boardcolor: this.state.boardcolor,
  }}
/>

This works but I understand this is not the best way to go. Wondering how I might destructure this in a more elegant way.这行得通,但我知道这不是 go 的最佳方式。想知道如何以更优雅的方式对其进行解构。

Destructuring this way works, and also you don't need to do key: key .以这种方式进行解构是可行的,而且您不需要执行key: key I'd say, do this way:我会说,这样做:

  const { boardid, boardvalue, boardcolor } = this.state;

  return (
    <div>
      <EditBoard
        boardArr={{
          boardid,
          boardvalue,
          boardcolor
        }}
      />
    </div>
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM