Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Best way to define a subarray of state with consideration of react/destructuring-assignment

I'd like to define an new array containing two values of state. This array will be send to the api. My first approach which was working fine, at first, was:

const userRoles = {
  userName: this.state.userName,
  roles: this.state.userRoles
};
putData('api/User/EditUserRoles', userRoles).then((result) => {
  const responseJson = result;
  if (responseJson) {
    this.setState({ msg: "User's roles updated successfully!" });
  }
});

but after i've enabled eslint - react/destructuring-assignment the following errors appeared:

  1. (property) userName: string Must use destructuring state assignmenteslintreact/destructuring-assignment
  2. (property) userRoles: string[] Must use destructuring state assignmenteslintreact/destructuring-assignment

so i changed the code to

const { userName, userRoles } = this.state;
const userNameBuffer = userName;
const userRolesBuffer = userRoles;
const userRolesArrayBuffer = {
  userName: userNameBuffer,
  roles: userRolesBuffer
};
putData('api/User/EditUserRoles', userRolesArrayBuffer).then((result) => {
  const responseJson = result;
  if (responseJson) {
    this.setState({ msg: "User's roles updated successfully!" });
  }
});

which workes, but i'm not happy about using the additional "buffer" variables. Is there a way to write this code more "handsome" (e.g. no "buffer" variables) with consideration of react/destructuring-assignment?

Thank you for every answer and sorry for any mistakes in my english that may occur.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you dont need to save vars in buffer, more elegant and common way is:

const { userName, userRoles } = this.state;
const userRolesBuffer = {
  userName, // its not mistake, in object variables with same name can be assigned like this
  roles: userRoles
};
putData('api/User/EditUserRoles', userRolesBuffer).then((result) => {
  const responseJson = result;
  if (responseJson) {
    this.setState({ msg: "User's roles updated successfully!" });
  }
});
about 4 years ago · Juan Pablo Isaza Report

0

Well the answer seems pretty easy and i misunderstood something.

I thought it's necessary to declare an array like

const exampleArray = {
    key1: "value1",
    key2: "value2"
}

that the result of JSON.stringify(exampleArray); looks like

{
     "key1": "value1",
     "key2": "value2"
}

but it seems like that

const key1 = "value1";
const key2 = "value2";
const exampleArray = {
      key1,
      key2,
}

does return the the same result...

so my code, matching react/destructuring-assignment finally looks like

const { userName, userRoles } = this.state;
const roles = userRoles;
putData('api/User/EditUserRoles', { userName, roles }).then((result) => {
  const responseJson = result;
  if (responseJson) {
    this.setState({ msg: "User's roles updated successfully!" });
  }
});

Thank you for your answers... especially Pavlo Demydiuk ^^

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!