从上下文使用者传递参数会导致“ TypeError”:...不是函数

时间:2019-05-20 12:45:14

标签: reactjs react-context

我正在使用提供程序与数据库进行交互并存储值,这些值将传递给使用者组件:

this.state = {
            ...
            abilities: {
                "bra": 0,
                "agi": 0,
                "int": 0,
                "cun": 0,
                "will": 0,
                "pre": 0
            },

使用者确实显示了值,现在我需要提供一种在提供程序中更改值的方法。在通过contextType传递函数之前,我已经做到了。现在,我还需要确定该对象的哪个值应该更改,并且我坚持传递一个使我能够执行此操作的值。我已经在提供程序中编写了此功能:

raiseAbility = ability => {
        console.log(ability)
        this.setState((state) => ({
            abilities: {...state.abilities, ability: ability + 1}
        }))
    }

当我尝试传递参数(处于提供者状态的对象的键)时,React告诉我该函数不是功能:

<Button ... onClick={this.context.raiseAbility("bra")}>+</Button>
...
TypeError: _this2.context.raiseAbility is not a function

在这种情况下,传回参数的最佳方法是什么?

0 个答案:

没有答案