反应不变性助手错误?

时间:2018-07-10 21:45:20

标签: reactjs immutability

此处的完整代码:https://github.com/dtdimart/hipaa-checker

这让我困了几个小时。本质上是此基于React的Quiz应用程序旧版本的复制品。该应用程序编译时没有错误,但是当单击答案时,它将引发错误

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-addons-update:

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);
}

0 个答案:

没有答案
相关问题