I am learning react and get stuck in the following line of code:
const { favourites } = this.state
Can someone please help me?
I am learning react and get stuck in the following line of code:
const { favourites } = this.state
Can someone please help me?
that isn't React-specific, it is a JavaScript (ES6) feature called destructuring:
What this means is that the const favorites is equal to the value of favorites in the state of the component. So if the value of favorites is 22 in the state, the const favorite is equal to 22.
const { favourites } = this.state
is same as
const favourites = this.state.favourites
Also, an important note is that by using const, you're making the state.favorites prop read-only. Also, you can use it for any object. Let's say I had an employee object with employeeId, lastName, firstName props, etc. Let's say I get an array of employee objects, I can use this construct to greatly simplify the code.
render() {
const {UserName, EmailAddress, AccountCreated, PasswordChanged, AccountName,
Locked, Enabled} = this.props.ewdsUser;
return (
<tr>
<td>{UserName}</td>
<td>{EmailAddress}</td>
<td>{AccountCreated}</td>
<td>{PasswordChanged}</td>
<td>{Locked}</td>
<td>{Enabled}</td>
This prevents me from having to do this:
render() {
return (
<tr>
<td>{this.props.UserName}</td>
<td>{this.props.EmailAddress}</td>
<td>{this.props.AccountCreated}</td>
<td>{this.props.PasswordChanged}</td>
<td>{this.props.Locked}</td>
<td>{this.props.Enabled}</td>