首页 > 解决方案 > 在组件的函数中调用道具函数“无法读取未定义的属性‘道具’”

问题描述

我有一个触发我的 onClick 功能的按钮:

<button id="closeModal" variant="danger" onClick={ this.onClick }></button>

我的 onClick 绑定在构造函数中:

this.onClick = this.handleSubmit.bind(this);

然后我指定调用两个函数的函数:一个来自 props,一个不是(它是 Formik 的函数)。

onClick(event) {
    this.handleSubmit.bind(this, values);
    this.props.closeModal;
}

Uncaught TypeError: Cannot read property '...' of undefined当我将它们切换到位置时,onClick 中的两个函数的错误是相同的。我希望按钮现在触发两个功能。以前,当我只有

<button id="closeModal" variant="danger" onClick={ this.props.closeModal }></button>

一切都很好。你知道如何解决这个问题吗?

标签: javascriptreactjsredux

解决方案


我认为你没有根据你的代码绑定正确的函数我希望看到这样的东西

this.onClick = this.onClick.bind(this)

代替

this.onClick = this.handleSubmit.bind(this)

你也需要打电话closeModal,所以它应该是

this.props.closeModal();


推荐阅读