【问题标题】:How to set array value in react native mobx?如何在反应原生 mobx 中设置数组值?
【发布时间】:2022-01-01 13:03:00
【问题描述】:

我有 favoriteStore,而且我是 mobx 的新手,我想将数组从 bookmarkapi 设置为 petList 数组,但我遇到了错误。

错误;

[Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_this2.userId')]

代码;

import { observable, computed, action, makeObservable, configure } from "mobx";
import { bookmarkApi, petApi } from '../../constants/apiURL';
import AsyncStorage from '@react-native-async-storage/async-storage';

configure({
    enforceActions: "never",
})

class Favorites {
    userId = 0;
    petList = [];

    constructor() {
        makeObservable(this, {
            userId: observable,
            petList: observable,
            bookMarkList: action
        })
    }

    bookMarkList = async () => {
        const value = await AsyncStorage.getItem('userId')
        if (value != null) {
            this.userId = value;
            console.log(this.userId);
        }

        bookmarkApi.get('/').then(function (responseJson) {
            this.petList = responseJson.data.filter(data => data.userId == this.userId)
        })
    }
}

export const favoriteStore = new Favorites();

【问题讨论】:

    标签: arrays react-native api mobx


    【解决方案1】:

    回调函数丢失上下文,只需使用箭头函数进行bookmarkApi.get回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-18
      • 2019-08-09
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多