简体   繁体   中英

MobX store is not updating after fetch request in React Native app

I created a MobX store with async fetch function. When i call it i get the data from API. And i checked the data in console, and it comes every time, but MobX store is not changing it still the same. Can you tell please me how can i solve it?

MobX store:

import { createContext } from 'react'
import { action, decorate, observable, computed, runInAction } from 'mobx'
import mapObjects from '../utils/mapObjects'

export class DataStore {
  data = null
  error = false
  loading = true


  async fetchData(url) {
    this.data = null
    this.loading = false
    try {
      console.log('TRY')
      const response = await fetch(url)
      const jsonResponse = await response.json()
      const obj = await mapObjects(jsonResponse)
      runInAction(() => {
        console.log('WRITE!!!')
        this.loading = false
        this.data = obj
      })
    } catch (err) {
      runInAction(() => {
        console.log(err)
        this.loading = false
        this.error = err
      })
    }
  }
}

decorate(DataStore, {
  data: observable,
  error: observable,
  loading: observable,
  fetchData: observable
})

export default createContext(new DataStore())

decorate(...)调用中,您使fetchData成为observable而不是action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM