javascript - 在组件的函数中调用道具函数“无法读取未定义的属性‘道具’”
问题描述
我有一个触发我的 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>
一切都很好。你知道如何解决这个问题吗?
解决方案
我认为你没有根据你的代码绑定正确的函数我希望看到这样的东西
this.onClick = this.onClick.bind(this)
代替
this.onClick = this.handleSubmit.bind(this)
你也需要打电话closeModal
,所以它应该是
this.props.closeModal();
推荐阅读
- xamarin - 无法设置 imagecropper.Forms 的来源
- npm - 如何将 package-lock.json 转换为 yarn.lock?
- perl - 使用 Tree::DAG_Node 将列表堆打印为树格式
- python - abort(400) 引发 500 内部服务器错误 - Flask 1.0
- sox - sox 转换为频谱图参数含义
- c# - 如何使两个物体反向绕一个点在曲线中这样做?统一,C#
- php - Wordpress 空白前空白页不起作用
- java - 抽象类的 Hibernate/JPA 映射
- c++ - 在基于范围的 for 循环中使用 shared_ptr 到 std::vector
- javascript - 将Javascript变量发送到文件中的PHP函数参数中?