3

I'm new in react and I'm reading some code i found this line of code:

const {intl: { formatMessage }, } = this.context

is a const declaration but i don't understand I know is JS ES6 but i don't understand what is it for? How can I check the value of this const?

thanks

Angelotti
  • 307
  • 2
  • 15

3 Answers3

5

As people already answered. This is object destructuring

Simple example: const contact = {name: 'John', email: 'john@doe.com'}

With ES6 you can do const {email} = contact; //email = contact.email In case you want to name the variable differently, it would be:

const {email: mailbox} = contact //equivalent to mailbox = contact.email;

Back to the original question: {intl: { formatMessage }, } = this.context

=> {formatMessage} = this.context.intl => formatMessage = this.context.intl.formatMessage

Jimmy
  • 186
  • 2
1

The code that you have is, actually a representation of the following code,

const formatMessage = this.context.intl.formatMessage

You can read about object destructuring to know more about it.

Rajaprabhu Aravindasamy
  • 63,064
  • 13
  • 90
  • 119
0

This simply means that this.context contains a structure similar to this

this.context = {
  intl:{
      formatMessage: // This has a value let's say "Hello"
  },
//Other key-value pairs could be contained in the context object }

This line of code is a shorthand syntax telling you that only the formatMessage property which can be found inside "intl" should be retrieved from the large object called "this.context"

herlarby
  • 81
  • 4