Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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
Reactjs React Native-一起使用Immer.js和SetState是否存在问题?_Reactjs_React Native_Immutability_Immer.js - Fatal编程技术网

Reactjs React Native-一起使用Immer.js和SetState是否存在问题?

Reactjs React Native-一起使用Immer.js和SetState是否存在问题?,reactjs,react-native,immutability,immer.js,Reactjs,React Native,Immutability,Immer.js,昨天,我有一个行为,我不明白使用Immer.js和setState在一起。在获取数据时,我使用了一个setState(顺便说一句,这是一种糟糕的方式),这个获取在我的SectionList的每个endreach处都被调用 此设置状态如下所示: this.setState((prev) => { let sections = prev.sections /* Extract alive topics from "topics"(array retrieved from fe

昨天,我有一个行为,我不明白使用Immer.js和setState在一起。在获取数据时,我使用了一个setState(顺便说一句,这是一种糟糕的方式),这个获取在我的SectionList的每个endreach处都被调用

此设置状态如下所示:

this.setState((prev) => {
    let sections = prev.sections

    /* Extract alive topics from "topics"(array retrieved from fetch)*/
    let topicsSection1 = topics.filter((card) => !card.states.is_killed)

    /* Extract killed topics from "topics"(array retrieved from fetch)*/
    let topicsSection2 = topics.filter((card) => card.states.is_killed)

    if (sections[0] && sections[0].data)
    sections[0].data = positionExtracted > 1 ? sections[0].data.concat(...topicsSection1) : topicsSection1

    if (sections[1] && sections[0].data)
    sections[1].data  = positionExtracted > 1 ? sections[1].data.concat(...topicsSection2) : topicsSection2

    return {
        sections: sections,
        position: response.position,
        lastPage: response.laftPage
    }
})
_onPressTopic = (id_card) => {
    this.setState(produce((draft) => {
        if (draft.sections[0] && draft.sections[0].data)
            draft.sections[0].data = draft.sections[0].data.map((item) => {
                if (item.id === id_card)
                    item.opened = !item.opened
                return item
            })
        if (draft.sections[1] && draft.sections[1].data)
            draft.sections[1].data = draft.sections[1].data.map((item) => {
                if (item.id === id_card)
                    item.opened = !item.opened
                return item
            })
    }))
}
一切都很顺利

但是,我有一个函数,当您按下主题时会调用它,它会更改数据数组中主题的“opened”值,以向列表指示“thisttopic”已打开。 此函数调用Immer.js的“product”函数

这个函数如下所示:

this.setState((prev) => {
    let sections = prev.sections

    /* Extract alive topics from "topics"(array retrieved from fetch)*/
    let topicsSection1 = topics.filter((card) => !card.states.is_killed)

    /* Extract killed topics from "topics"(array retrieved from fetch)*/
    let topicsSection2 = topics.filter((card) => card.states.is_killed)

    if (sections[0] && sections[0].data)
    sections[0].data = positionExtracted > 1 ? sections[0].data.concat(...topicsSection1) : topicsSection1

    if (sections[1] && sections[0].data)
    sections[1].data  = positionExtracted > 1 ? sections[1].data.concat(...topicsSection2) : topicsSection2

    return {
        sections: sections,
        position: response.position,
        lastPage: response.laftPage
    }
})
_onPressTopic = (id_card) => {
    this.setState(produce((draft) => {
        if (draft.sections[0] && draft.sections[0].data)
            draft.sections[0].data = draft.sections[0].data.map((item) => {
                if (item.id === id_card)
                    item.opened = !item.opened
                return item
            })
        if (draft.sections[1] && draft.sections[1].data)
            draft.sections[1].data = draft.sections[1].data.map((item) => {
                if (item.id === id_card)
                    item.opened = !item.opened
                return item
            })
    }))
}
我的问题是:

如果我打开一个主题,然后我的列表数据通过这个函数,那么当再次调用EndReach时,要么我得到一个错误“这个对象不昂贵”,要么我的列表数据根本没有被修改。如果我不使用我的第一个设置状态,而是使用Immer的产品,一切都会恢复正常

我不明白的是:如果我只使用Immer.js或SetState,为什么一切都能完美地工作,但当我尝试同时使用这两个工具时,它们似乎就不合拍了

谢谢你的回答

我希望我说清楚了

维克多