reactjs - 在 onClick 事件之前在页面加载时调用 Reactjs 函数
问题描述
功能在页面加载我的代码时工作,如下
所示
import React, { Component } from "react";
import ExtnButton from "./Button";
class MovieList extends Component {
handleDelete = index => {
console.log("inside handleDelete:");
};
render() {
return (
<React.Fragment>
<ExtnButton handleDelete={this.handleDelete} index={index} />
</React.Fragment>
);
}
}
export default MovieList;
孩子
import React, { Component } from "react";
class Button extends Component {
state = {};
render() {
return (
<button
onClick={this.props.handleDelete(this.props.index)}
className="btn btn-danger"
>
Delete
</button>
);
}
}
export default Button;
但是在页面加载函数handleDelete调用时没有任何点击事件
解决方案
错误的:
onClick={this.props.handleDelete(this.props.index)}
正确的:
onClick={() => this.props.handleDelete(this.props.index)}
推荐阅读
- javascript - JavaScript - 通过 cmd 提问并存储对变量的响应。将此代码添加到现有程序中
- swift - 无法将“Double”类型的值分配给“String”类型?
- python - 组 2 列并基于组值取基于特定值的组
- php - 来自 laravel 的注册视图不显示和识别来自我的控制器 Laravel 6.0 的变量
- scala - 如何对嵌套案例对象使用播放格式序列化/反序列化宏
- javascript - 接收和发送表单数据
- security - 如果跨组织启用 chrome 开发工具,是否存在任何潜在的安全问题?
- java - RxJava - 如果其中一个可观察对象导致错误,如何使平面图继续?
- python - 基于列值着色表行
- r - 如何在 R 中创建置信区间图