-1

I'm trying to use setState after I did a fetch on an api so I can load it into react. The problem is that it is a async function so I can't use it inside the class itself. How can I still change the state from outside the class itself?

I have a function async function getUsers() {} and in the class that's just under it starte = {data: ''}

  • 2
    Please, provide https://stackoverflow.com/help/mcve for your problem. It's unclear what your code looks like. *it is a async function so I can't use it inside the class itself* - how does the fact that it's async prevent it from being used? *How can I still change the state from outside the class itself?* - you usually shouldn't, all `state` logic belongs to a class. – Estus Flask Feb 28 '19 at 11:29

2 Answers2

0

Call the method from within the class and you will know when it is done so that you may call setState.

Marcus
  • 164
  • 1
  • 13
0

You can write a function that deals with updating the state and pass that method to the getUsers function and trigger the callback from there.

Johnson Abraham
  • 331
  • 2
  • 5