Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';在订阅主体内的setState钩子中的上一个状态上调用对象方法_Javascript_Reactjs_Rxjs_Setstate - Fatal编程技术网

Javascript Can';在订阅主体内的setState钩子中的上一个状态上调用对象方法

Javascript Can';在订阅主体内的setState钩子中的上一个状态上调用对象方法,javascript,reactjs,rxjs,setstate,Javascript,Reactjs,Rxjs,Setstate,尝试使用useState钩子更新对象状态在RxJS subject subscribe主体内部不起作用,但在其外部起作用。(用第二代码部分中的注释注释) 我收到的错误是:TypeError:eventMap.addEvent不是函数 我的目标是使用backpageMessageSubject$中的事件从EventMap调用addEvent,然后将返回的数组从getNthEvent方法传递给子组件 class EventMap { eventArrays; constructo

尝试使用useState钩子更新对象状态在RxJS subject subscribe主体内部不起作用,但在其外部起作用。(用第二代码部分中的注释注释)

我收到的错误是:TypeError:eventMap.addEvent不是函数

我的目标是使用backpageMessageSubject$中的事件从EventMap调用addEvent,然后将返回的数组从getNthEvent方法传递给子组件

class EventMap {
     eventArrays;
     constructor() {
         this.eventArrays = new Map();
     }

    addEvent = (id, data) => {
        this.eventArrays.has(id)
            ? this.eventArrays.get(id).push(data)
            : this.eventArrays.set(id, [data]);
        return this.eventArrays;
    }

    getNthEvent = (n) => {
        const result = [];
        for (let key of this.eventArrays.keys()) {
            result.push(this.eventArrays.get(key)[n]);
        }
        return result;
    }
}
主题来源:

export const backpageMessageSubject$ = new Subject();

完成
setEventMap(eventMap=>eventMap.addEvent(1,1))
后,事件映射的状态是
eventMap.addEvent
返回值,它不是
eventMap
实例。因此,当您稍后尝试使用它时,它会失败,因为您正在使用的(映射)没有添加事件

由于不能直接修改状态中的项,因此需要更新
EventMap
,以便它返回带有添加事件的映射的新实例,而不是按照以下方式修改现有映射:

class EventMap {
     eventArrays;
     constructor(eventArrays = new Map()) { // Note accepting a parameter
         this.eventArrays = eventArrays;
     }

    addEvent = (id, data) => {
        // Copy the current map, reusing the same arrays
        const eventArrays = new Map(this.eventArrays);
        // Get the current array for this `id`
        const current = eventArrays.get(id);
        if (!current) {
            // We don't have it, create a new one in the new map
            eventArrays.set(id, [data]);
        } else {
            // We have it, copy its contents to a new array and
            // add the new data to the end, storing it in the
            // new map
            eventArrays.set(id, [...current, data]);
        }
        // Create a new EventMap with those arrays and return it
        return new EventMap(eventArrays);
    }

    getNthEvent = (n) => {
        const result = [];
        for (let key of this.eventArrays.keys()) {
            result.push(this.eventArrays.get(key)[n]);
        }
        return result;
    }
}

完成
setEventMap(eventMap=>eventMap.addEvent(1,1))
后,事件映射的状态是
eventMap.addEvent
返回值,它不是
eventMap
实例。因此,当您稍后尝试使用它时,它会失败,因为您正在使用的(映射)没有添加事件

由于不能直接修改状态中的项,因此需要更新
EventMap
,以便它返回带有添加事件的映射的新实例,而不是按照以下方式修改现有映射:

class EventMap {
     eventArrays;
     constructor(eventArrays = new Map()) { // Note accepting a parameter
         this.eventArrays = eventArrays;
     }

    addEvent = (id, data) => {
        // Copy the current map, reusing the same arrays
        const eventArrays = new Map(this.eventArrays);
        // Get the current array for this `id`
        const current = eventArrays.get(id);
        if (!current) {
            // We don't have it, create a new one in the new map
            eventArrays.set(id, [data]);
        } else {
            // We have it, copy its contents to a new array and
            // add the new data to the end, storing it in the
            // new map
            eventArrays.set(id, [...current, data]);
        }
        // Create a new EventMap with those arrays and return it
        return new EventMap(eventArrays);
    }

    getNthEvent = (n) => {
        const result = [];
        for (let key of this.eventArrays.keys()) {
            result.push(this.eventArrays.get(key)[n]);
        }
        return result;
    }
}