Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.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 反应不变性辅助错误?_Reactjs_Immutability - Fatal编程技术网

Reactjs 反应不变性辅助错误?

Reactjs 反应不变性辅助错误?,reactjs,immutability,Reactjs,Immutability,完整代码如下: 这让我困了好几个小时。本质上是这个基于React的测验应用程序的旧版本的复制品。应用程序编译时没有错误,但单击答案时会抛出错误 Cannot read property '**depends on answer picked**' of undefined 我确信错误源于以下内容的“更新”部分: setUserAnswer(answer) { const updatedAnswersCount = update(this.state.answersCount,

完整代码如下:

这让我困了好几个小时。本质上是这个基于React的测验应用程序的旧版本的复制品。应用程序编译时没有错误,但单击答案时会抛出错误

Cannot read property '**depends on answer picked**' of undefined
我确信错误源于以下内容的“更新”部分:

    setUserAnswer(answer) {
    const updatedAnswersCount = update(this.state.answersCount, {
        [answer]: { $apply: currentValue => currentValue + 1 }
    });

    this.setState({
        answersCount: updatedAnswersCount,
        answer: answer
    });
}
对于更新,我使用的是immutability helper,而不是旧版react插件更新:

import update from "immutability-helper";
主要错误引用了不变性帮助程序代码本身,具体如下:

var nextValueForKey =
    type(object) === 'Map'
       ? update(object.get(key), spec[key])
       : update(object[key], spec[key]);
如果有任何帮助,我们将不胜感激,因为这应该是我的应用程序功能在进入定制/外观之前的最后一步

这会是一个棘手的问题吗?建造商是:

    constructor(props) {
    super(props);

    this.state = {
        counter: 0,
        questionId: 1,
        question: "",
        answerOptions: [],
        answer: "",
        answerCount: {
            NonCompliant: 0,
            Compliant: 0,
            NeedsAttention: 0
        },
        result: ""
    };
    this.handleAnswerSelected = this.handleAnswerSelected.bind(this);
}

更新前状态的值是多少?你确定你的方法中正确绑定了它吗?Axnyff,它是一个空字符串。对于绑定,这是我的另一个问题,我将使用我的构造函数更新该问题。因此,this.state.answersCount未定义,并且没有
[answer]
属性?