這篇文章給大家分享的是有關在React中綁定this作用域并傳參的解決方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
在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} changeMsg1(){ console.log(this) this.setState({ msg:"Way1" }) } }
{this.state.msg}