1.在方法里,是直接获取不到this.state属性的,我们有3种方式解决:
a)给方法添加bind(this),改变this指向,如截图里的getData方法
b)在构造函数里添加引用绑定,改变this指向,如截图里的getMessage方法
c)执行方法用箭头函数改变this指向,如截图里的getDataMessage方法

2.改变state的值
使用setState的方法

3.执行方法传值
调用的时候,跟我们1. a)中的一样,要使用bind(this),如截图里的sendMsg方法

网友评论