[英]MobX store is not updating after fetch request in React Native app
I created a MobX store with async fetch function. 我创建了一个具有异步提取功能的MobX存储。 When i call it i get the data from API.
当我调用它时,我从API获取数据。 And i checked the data in console, and it comes every time, but MobX store is not changing it still the same.
我在控制台中检查了数据,每次都来了,但是MobX存储并没有改变它。 Can you tell please me how can i solve it?
您能告诉我如何解决吗?
MobX store: 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(...)
调用中,您使fetchData
成为observable
而不是action
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.