likes
comments
collection
share

彻底搞懂React类组件中的实例属性state!

作者站长头像
站长
· 阅读数 29

往期回顾:

本教程可以帮你更好的理解useState函数的一些渲染原理!

类组件中的属性

我们看一个简单的类组件实例

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>hello_react</title>
  </head>
  <body>
    <!-- 准备好一个“容器” -->
    <div id="test"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="./js/react.development.js"></script>
    <!-- 引入react-dom,用于支持react操作DOM -->
    <script type="text/javascript" src="./js/react-dom.development.js"></script>
    <!-- 引入babel,用于将jsx转为js -->
    <script type="text/javascript" src="./js/babel.min.js"></script>
    <script type="text/babel">
      // 1、创建类组件
      class MyComponent extends React.Component {
        render() {
          console.log("render中的this", this);
          return <h1>今天天气很热,我想吃冰激凌</h1>;
        }
      }
      // 渲染组件
      ReactDOM.render(<MyComponent />, document.getElementById("test"));
    </script>
  </body>
</html>

可以看到,类组件继承了父类的React.Component的若干属性,其中props、refs、state是我们需要深入学习的。彻底搞懂React类组件中的实例属性state!

我们来看看state的用法

state的基础用法

   // 1、创建类组件
  class MyComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        isHot: true,
        food: "冰棒",
      };
    }
    render() {
      console.log("render中的this", this);
      return (
        <h1>
          今天天气很{this.state.isHot ? "热" : "冷"},我想吃{this.state.food}
        </h1>
      );
    }
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));

页面效果:

彻底搞懂React类组件中的实例属性state!

上述示例中,我们显示的写了constructor构造函数,这是使用state必须做的事情,下面是最基础的代码结构

constructor(props) {
  super(props);
  this.state = {
    //自定义属性
  };
}

我们暂不讨论props是什么东西。state这个对象上,可以自定义我们的属性。我们观察下this(组件实例)

彻底搞懂React类组件中的实例属性state!

可以发现,此时实例state上存在我定义的属性了。

那我们我们如何向vue一样,单击某个按钮来更新state中的属性呢?我们先学习一下简单学一下react中的事件绑定

事件绑定

原生事件绑定

react中,我们依旧可以使用原生事件绑定的方法,如这样

<script type="text/babel">
  // 1、创建类组件
  class MyComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        isHot: true,
        food: "冰棒",
      };
    }
    render() {
      console.log("render中的this", this);
      return (
        <h1 id="title">
          今天天气很{this.state.isHot ? "热" : "冷"},我想吃{this.state.food}
        </h1>
      );
    }
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));
  const title = document.getElementById("title");
  title.addEventListener("click", () => {
    alert("标题被点击了");
  });
</script>

彻底搞懂React类组件中的实例属性state!

当然,下面这种写法也是原生事件绑定的一种写法

  title.onclick = function () {
    alert("标题被点击了");
  };

不过,既然我们都学习react,肯定不推荐原生写法了。

react中的事件绑定

react中绑定事件很容易

<script type="text/babel">
  // 1、创建类组件
  class MyComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        isHot: true,
        food: "冰棒",
      };
    }
    render() {
      console.log("render中的this", this);
      return (
        <h1 onClick={tel}>
          今天天气很{this.state.isHot ? "热" : "冷"},我想吃{this.state.food}
        </h1>
      );
    }
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));
  function tel() {
    alert("我被点击了!");
  }
</script>

彻底搞懂React类组件中的实例属性state!

但要注意:

  • JSX中的onClick中是驼峰命名,和原生的onclick是不一致的!
  • onClick表达式中的函数只能写函数名!

如果写了函数和括号  <h1 onClick={tel()}> , 那么函数会自己执行

彻底搞懂React类组件中的实例属性state!

那么,我们如何通过事件绑定实现更改state中的值呢?

通过事件绑定修改state的值

一个最基本的代码应该是这样的

<script type="text/babel">
      // 1、创建类组件
      class MyComponent extends React.Component {
        constructor(props) {
          super(props);
          this.state = { isHot: true };
          this.tel = this.tel.bind(this);
        }
        render() {
          return <h1 onClick={this.tel}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
        }
        tel() {
          const isHot = this.state.isHot;
          this.setState({ isHot: !isHot });
        }
      }
      // 渲染组件
      ReactDOM.render(<MyComponent />, document.getElementById("test"));
    </script>

彻底搞懂React类组件中的实例属性state!

上述代码中,我们将tel函数放在了组件类里,同时通过this.tel = this.tel.bind(this);改变了类中的this指向;最后通过setState更新了state中的isHot值。

要想彻底理解上述代码的含义,我们需要深入学习react组件中的this指向问题。

类组件中的this指向

函数中的this指向

<script type="text/babel">
  // 1、创建类组件
  class MyComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = { isHot: true };
    }
    render() {
      return <h1 onClick={this.tel}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
    }
    tel() {
      console.log("类中的this", this);
    }
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));
</script>

我们直接打印下this的值

彻底搞懂React类组件中的实例属性state!

可以看到,当函数tel被执行时,其代码中的this指向是undefined

根据类的知识,我们能梳理出一下信息:

  • tel方法是定义在MyComponent的原型对象上,供实例使用
  • tel方法作为onClick的回调,不是由实例对象直接调用的,而是onClick直接调用的

我们知道, this的指向本质上是指向调用者的,即谁调用函数或方法,this就指向这个调用者!由于tel函数不是实例调用的,因此,必然没有指向MyComponent,this也就是undefined了。

更改函数中this的指向

我们需要通过函数来更改state中的值,必须在函数中可以访问组件实例对象才行。因此,我们需要通过bind改变其this指向

我们先复习一下bind的用法

bind(thisArg, arg1, arg2, arg3, ...)

fn.bind的作用是只修改this指向,但不会立即执行fn;会返回一个修改了this指向后的fn。需要调用才会执行:bind(thisArg, arg1, arg2, arg3, ...)()。bind的传参和call相同。

现在,我们在看这句代码,它的作用很明显

this.tel = this.tel.bind(this);

彻底搞懂React类组件中的实例属性state!

通过 this.telMyComponent上创建了一个tel属性,这个属性值是一个函数,函数来自MyComponen原型上的方法tel,且函数tel的this指向MyComponent自身

或者,我们这么写更便于理解

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { isHot: true };
    this.telFuc = this.tel.bind(this);
  }
  render() {
    return <h1 onClick={this.telFuc}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
  }
  tel() {
    console.log(this);
    const isHot = this.state.isHot;
    this.setState({ isHot: !isHot });
  }
}

我们通过bind可以访问到MyComponent实例,从而便于更改state的值

彻底搞懂React类组件中的实例属性state!

更改类中的state状态值

根据最初的示例代码,我们知道,state状态值的更改是借助setState函数完成的,我们如果想使用react的双向数据绑定,就不能直接修改state的值,比如下面的我种方式不会让页面在点击时有视图更新

  // 1、创建类组件
  class MyComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = { isHot: true };
      this.telFuc = this.tel.bind(this);
    }
    render() {
      return <h1 onClick={this.telFuc}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
    }
    tel() {
      this.state.isHot = !this.state.isHot;
    }
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));

setState函数来自承自父类React.Component原型上的方法,因此,我们可以直接访问到。

彻底搞懂React类组件中的实例属性state!

更新状态值的基础方法如下

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { isHot: true };
    this.telFuc = this.tel.bind(this);
  }
  render() {
    return <h1 onClick={this.telFuc}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
  }
  tel() {
    console.log(this);
    this.setState({ isHot: !this.state.isHot });
  }
}

构造函数中每个函数执行的次数

<script type="text/babel">
  // 1、创建类组件
  class MyComponent extends React.Component {
    constructor(props) {
      console.log("constructor执行了");
      super(props);
      this.state = { isHot: true };
      this.telFuc = this.tel.bind(this)  }
    render() {
      console.log("render函数执行了");
      return <h1 onClick={this.telFuc}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
    }
    tel() {
      console.log("tel函数执行了");
      this.setState({ isHot: !this.state.isHot });
    }
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));
</script>

彻底搞懂React类组件中的实例属性state!

通过上图我们可以看出,由于MyComponent组件被实例化一次,因此constructor函数执行一次

render函数和原型上的 tel方法则在每次点击页面时执行一次

state的简写方法

state更改值的普通写法

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>hello_react</title>
  </head>
  <body>
    <!-- 准备好一个“容器” -->
    <div id="test"></div>
    <!-- 引入react核心库 -->
    <script type="text/javascript" src="./js/react.development.js"></script>
    <!-- 引入react-dom,用于支持react操作DOM -->
    <script type="text/javascript" src="./js/react-dom.development.js"></script>
    <!-- 引入babel,用于将jsx转为js -->
    <script type="text/javascript" src="./js/babel.min.js"></script>
    <script type="text/babel">
      // 1、创建类组件
      class MyComponent extends React.Component {
        constructor(props) {
          super(props);
          this.state = { isHot: true };
          this.telFuc = this.tel.bind(this);
        }
        render() {
          return <h1 onClick={this.telFuc}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
        }
        tel() {
          this.setState({ isHot: !this.state.isHot });
        }
      }
      // 渲染组件
      ReactDOM.render(<MyComponent />, document.getElementById("test"));
    </script>
  </body>
</html>

要更改一个组件中的状态值state,我们需要写一个构造器,在构造器内部初始化state

this.state = { isHot: true };

同时,需要更改MyComponent原型上函数tel的this指向

this.telFuc = this.tel.bind(this);

state更改值的简便写法

要精简这种写法,我们先复习下类的写法。

class Parent {
  constructor(name) {
    this.name = name;
  }
  speak() {
    console.log("请讲话");
  }
  age = 3;
  eat = function () {
    console.log("吃饭");
  };
}
const people = new Parent("小明");
console.log("people: ", people);

通过上述代码,我们可以发现,通过constructor我们可以在类自身添加静态属性,通过 age = 3;的形式也可以直接在类上自身添加属性;我们如果直接在类里写函数,则函数是定义在Parent类的原型prototype上的,但如果通过eat = function () {}; 的形式可以直接在类自身添加静态方法。

彻底搞懂React类组件中的实例属性state!

根据这个简写规则,我们可以直接更改类中state更新的代码

<script type="text/babel">
  // 1、创建类组件
  class MyComponent extends React.Component {
    state = { isHot: true };
    render() {
      return <h1 onClick={this.tel}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
    }
    tel = function () {
      this.setState({ isHot: !this.state.isHot });
    };
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));
</script>

这样看起来代码精简多了!但很可惜,this.setState({ isHot: !this.state.isHot });这句代码不会生效,因为这里的this依旧找不到。

但是,我们将function改成箭头函数的形式就好了

<script type="text/babel">
  // 1、创建类组件
  class MyComponent extends React.Component {
    state = { isHot: true };
    render() {
      return <h1 onClick={this.tel}>今天天气很{this.state.isHot ? "热" : "冷"},我想吃冰激凌</h1>;
    }
    tel = () => {
      console.log(this);
      this.setState({ isHot: !this.state.isHot });
    };
  }
  // 渲染组件
  ReactDOM.render(<MyComponent />, document.getElementById("test"));
</script>

使用箭头函数时,函数内的this指向父级,也就是MyComponent,因此,这个问题就迎刃而解了。

页面效果:彻底搞懂React类组件中的实例属性state!