MobX 商店在 React Native 应用程序中发出获取请求后不会更新



我创建了一个带有异步获取功能的MobX存储。当我调用它时,我从 API 获取数据。我在控制台中检查了数据,它每次都会出现,但 MobX 商店并没有改变它仍然是一样的。你能告诉我我该如何解决它吗?

MobX 商店:

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(...)呼叫中,您正在fetchDataobservable而不是action

相关内容

  • 没有找到相关文章

最新更新