-1

I have an array of objects and I am trying to destructure it so that I can directly use the property names in react-table's column's accessor property.

This is the raw JSON which is actually being fetched via an API call.

Not sure, where I am going wrong though.

Nauman
  • 894
  • 4
  • 14
  • 45
  • 4
    Your `let [` (or `const [`) is inside the body of a class, so it's not valid place to declare a *variable*. – VLAZ Jul 15 '20 at 13:27
  • @VLAZ right. ok. can you help me with a working example of sorts? maybe edit my `codeSandbox` thanks. – Nauman Jul 15 '20 at 13:32

1 Answers1

2

You can take sites in your state in the handleGetmeasurementsDataInfo() this way :

handleGetmeasurementsDataInfo = async function() {
await axios

  .get("https://run.mocky.io/v3/7c15d8c2-e946-40f4-a1b9-a9a53436b388")
  .then(response => {
    // handle success
    console.log("measurement data:", response.data);
    const sites = response.data.map(e => e.sites);
    this.setState({
      isLoading: false,
      measurementsData: response.data,
      sites: sites
    });
    console.log("Sites: ", this.state.sites);
  })
  [...]

You can then access it from your state.

Codebox

BSYoann
  • 156
  • 5