子組件input框觸發(fā)onChange方法時(shí)棍厌,實(shí)際調(diào)用父組件上changeHandle方法惯吕,如直接在onChange方法中調(diào)用this.aaa颁独,則this指向window。不能正確實(shí)現(xiàn)想要的效果吨枉。改變this有三種方法:
1蹦渣、用箭頭函數(shù)
子組件
父組件
在箭頭函數(shù)(e)=>this.aaa(e)中調(diào)用。
2貌亭、在構(gòu)造函數(shù)中用bind方法綁定
constructor(props){
? ? super(props);
? ? this.aaa=this.aaa.bind(this)
? }
onChange={this.aaa};
3柬唯、在onChange方法中用bind方法綁定
onChange={this.aaa.bind(this)};
親測(cè)三種方法都可實(shí)現(xiàn)。