javascript - ReactJs 事件绑定
问题描述
import React, { Component } from 'react';
class SayHello extends React.Component {
constructor(props) {
super(props);
this.state = { message: 'Hello!' };
// This line is important!
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log(this.state.message);
}
render() {
return <button onClick={this.handleClick}>Say hello</button>;
}
}
export default SayHello;`
为什么我们需要绑定?我理解上下文,但不会this
引用实例,有人可以用非反应方式或简单的 JS 类的例子来解释。
解决方案
推荐阅读
- angular - Angular 6:输入初始值,没有双向数据绑定
- python - 将实时流发送到单独的前端
- matlab - 从给定坐标列表的数组中提取值列表
- css - 修复了未知宽度元素上的菱形剪辑路径?
- ios - 由于 iOS 中“不同”视图层次结构中的锚点,自动布局约束崩溃,但是
- python - 当 Python 中的单独数组相等时,将变量设置为彼此相等
- matlab - 如何在 MATLAB 中生成具有给定均值、方差、偏斜和峰度的分布?
- sqlite - 查询在 SQLite 中不起作用
- ckan - CKAN DCAT 插件使用后如何查看我的文件
- visual-studio-2015 - 如何让 SSDT Publish 不生成 REVOKE CONNECT 脚本