react里面bind与箭头函数

  1. bind 由于在类中,采用的是严格模式,所以事件回调的时候,会丢失this指向,指向undefined,需要使用bind来给函数绑定上当前实例的this指向;
    箭头函数的this指向上下文,所以永久能拿到当前组件实例,this指向,我们可以完美的使用箭头函数来替代传统事件处理函数的回调

  2. 箭头函数
    class MyComponent extends React.Component {
    handleClick = () => {
    // 处理点击事件的代码
    }

render() {
return (

);
}
}

3 bind
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}

handleClick() {
// 处理点击事件的代码

原文链接:https://blog.csdn.net/qq_39207066/article/details/133678289

posted @   小白张先生  阅读(84)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示