0

How will I able to save in hooks that data from database. Since I need to display the data that I get to dropdown.

Here's my code

const [dataSystem, setdataSystem] = useState([])

const getAllSystems = async() => {
 ......
}

const getDependentSystems = async() => {
  await axios.get('/API' + ID)
  .then((response) => {
  console.log('LIST OF SYSTEM', response.data)
  setdataSystem(response.data)
  })
}

Since upon setState, data is not yet saved to dataSystem I need to trigger getDeoendetSystems() twice to display the list on my dropdown.

Result of console.log

LIST OF SYSTEM [{...},{...}]
  0: {ID: 1, SYSTEMID: 12 ...},
  1: {ID: 2, SYSTEMID: 13 ...}

Thank you

JayDee
  • 92
  • 1
  • 9
  • You're using await in a async function but you use .then to get the value and push it to the state ? You'll get the same result by removing async and await from your below code. If you can provide us a snippet of the code it'll be easier to debug – Nicolas Menettrier Aug 20 '21 at 08:53
  • You have two options, either wrap the `getDependentSystems` call in a proper `useEffect` with `dataSystem` as a dependency. Or merge the two axios calls into one operation. My suggestion would be the latter, if those two state variables should always change together. – Yoshi Aug 20 '21 at 09:02

0 Answers0