Amir Shahbabaie
Amir Shahbabaie

Reputation: 1412

Deconstruct and convert in React

A react component receives props and I'm deconstructing it like this:

const { ram, core } = this.props;

But I want to divide the ram value by 1024, of course I could go with:

let { ram, core } = this.props;
ram /= 1024;

Can I do it in one line and use const too?

Upvotes: 2

Views: 108

Answers (1)

Fraction
Fraction

Reputation: 12964

You can do it this way:

const { ram, core } = ( ({ ram, core }) => ({ ram: ram/1024, core }) )(this.props);

const props = { ram: 2048, core: 7 };


const { ram, core } = ( ({ ram, core }) => ({ ram: ram/1024, core }) )(props);

console.log(ram);

Upvotes: 2

Related Questions