[英]React Native - Mobx how to initialize store with remote data
我如何初始化 mobx 存儲並從我的 api 服務器加載數據。 我必須請求一個 api 服務器來獲取產品數據並將該數據放入可觀察數組中。 然后我將使用計算函數來查詢該數組(例如,按類別名稱獲取產品)
我已經做了一些工作,但商店將被初始化,但我的 Flatlist 項目不會在應用程序啟動時看到。 它只是在重新啟動應用程序后工作。
使用 api 調用查看這個簡單的 mobx 商店。 請記住定義您的 api 調用函數或相應地導入它。
// mobx Store
class Store extends BaseStore {
constructor() {
super();
this.getCustomers(); // initialize store observable
}
@observable customers = [];
@action getCustomers = () => {
this.customers = getList("customers"); // api call
return this.customers;
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.