dangre00
dangre00

Reputation: 450

React destructuring state to pass into component props

I am passing in part of my state into component props like this:

<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.

Upvotes: 1

Views: 48

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

Destructuring this way works, and also you don't need to do key: key. I'd say, do this way:

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

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

Upvotes: 2

Related Questions