Skip to content

Pls help with fetch #2994

Answered by kubk
Potiekhin asked this question in General
Jun 19, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

You are losing this in your example. It looks like you are calling getUsers like this:

<Button onClick={userStore.getUsers}/>

This won't work because of the JS nature. There are few ways to fix it:

  1. Prevent losing this:
<Button onClick={() => userStore.getUsers()}/>
  1. Convert your method to arrow function:
<Button onClick={userStore.getUsers}/>

...

class UserStore {
  ...
  getUsers = () => {
    ...
  }
}
  1. Use Mobx autobind functionality:
<Button onClick={userStore.getUsers}/>

...

class UserStore {
  ...
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true })
  }
  
  getUsers() {
    ...
  }
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Potiekhin
Comment options

Answer selected by iChenLei
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants