0

I would like to write those two lines as one:

const { page, totalPages, changePage } = this.props;
const pagination = { page, totalPages, changePage };

I could not find anywhere how to do it, it only makes sense that I would be able to do it in one line.

I was hoping for something like

const pagination = { page, totalPages, changePage } = this.props;
Andreykul
  • 233
  • 3
  • 8

2 Answers2

1

No, you can't do it the way your are right now. You're abusing destructuring which is supposed to be used to take fields from an object and turn them into local variables. You're doing this and then immediately converting them back into an object.

To do this in one, step, simply make a new object and reference the fields you want from the old object.

const pagination = { page: this.props.page, totalPages: this.props.totalPages, changePage: this.props.changePage }
Soviut
  • 79,529
  • 41
  • 166
  • 227
  • the `const` should take care of the `useless` variable. I just don't wont to repeat the `this.props` over and over again, and was hoping there is a simple way to extract those three properties into a separate object. – Andreykul Jun 19 '17 at 18:50
  • 1
    @Andreykul You can't. You're looking for syntactic sugar that doesn't exist. What I've shown you IS the simplest way. – Soviut Jun 19 '17 at 19:10
  • was hoping for some sugar :( – Andreykul Jun 19 '17 at 19:16
0

You can assign like this, But I don't understand the reason for assigning like this.

const pagination = { page, totalPages, changePage } = this.props;
Mayank Shukla
  • 80,295
  • 14
  • 134
  • 129
M.Navy
  • 147
  • 6