这篇文章给大家分享的是有关在React中绑定this作用域并传参的解决方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

在React中时常会遇到this指向的作用域问题 从而导致undefined报错
先来个Demo:
功能很简单 点击按钮改变文字
import React from 'react';
export default class BindWithThis extends React.Component {
constructor(props) {
super(props);
this.state = {
msg:"BindWithThis"
}
}
render() {
return
{this.state.msg}
}
changeMsg1(){
console.log(this)
this.setState({
msg:"Way1"
})
}
}