React 拾遗:类作为组件 (1)

2017-12-06  本文已影响0人  Tim_Lee

如何使用代码

安装项目前置依赖,以及启动项目的方法,参看:React 拾遗:项目脚手架

请根据文章内容,把相应部分的代码注释取消,即可运行。

摘要

本文介绍

项目代码地址:React 拾遗:类作为组件 (1)

JavaScript 类与面向对象

面向对象编程(Object-Oriented Programming, OOP)是一个古老的概念。OOP 要解决的一个问题是:

不管是 C++ 还是 Java 提到的类与实例,或者 Python 的 Metaclass,亦或是 JavaScript 的原型及其扩展,都是为了解决上述面向对象要解决的问题。

关于 JavaScript 的面向对象与原型链,可以在以下内容中找到详细叙述,本文不再赘述。

React 中的类组件

1 最简单的 React 类组件

React 的最简单的类组件如下所示:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class App extends Component {
  render() {
    return (
      <div>
        <h1>Title - main title</h1>
        <p>The content part</p>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

2 类组件的属性与状态(state)

(1) render() 方法中的变量

为了动态显示 JSX 内容,在 render() 方法中可以使用变量,如下所示:

class App extends Component {
  render() {
    const title = 'main title';
    return (
      <div>
        <h1>Title - {title}</h1>
        <p>The content part</p>
      </div>
    );
  }
}

但是 render() 方法中的变量,通常有两种用途:

(2) constructor() 中的变量

如果要设置一个变量,让类中所有方法都能使用,就是放在构造器 constructor() 中。构造器中的变量,在类创建时就同时创建完成,可以在整个类中使用。

class App extends Component {
  constructor() {
    super()
    this.title = 'title in constructor'
  }

  render() {
    return (
      <div>
        <h1>Title - {this.title}</h1>
        <p>The content part</p>
      </div>
    );
  }
}

两个注意点:

class App extends Component {
  constructor(props) {
    super(props)
    //...
  }
  //...
}

(3) React 类组件的状态

React 类组件一个非常重要的内容,就是维护自己的内部状态(state)。这个状态需要放在 constructor() 中。类中的重要属性(需要保持的数据),基本都是放在状态中。

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      title: 'title in constructor',
      person: {
        name: 'Jim',
        age: 25,
        location: 'China'
      }
    };
  }

  render() {
    return (
      <div>
        <h1>Title - {this.state.title}</h1>
        <p>The content part</p>
        <ul>
          <li>Name: {this.state.person.jim}</li>
          <li>Age: {this.state.person.age}</li>
          <li>Location: {this.state.person.location}</li>
        </ul>
      </div>
    );
  }
}

类中状态的修改

1 使用 .setState() 修改 state

首先,** 永远不要直接修改 state **。请一定使用.setState() 修改 state 。

下面实现的功能是,有一个新增加的按钮,点击以后,标题会从 'title in constructor' 更改成 'The changed title'。constructor()中、新方法、JSX中处理都有变化,后面会讲解。

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class App extends Component {
  constructor(props) {
    super(props);
    this.changeNameHandler = this.changeNameHandler.bind(this);
    this.state = {
      title: 'title in constructor',
      person: {
        name: 'Jim',
        age: 25,
        location: 'China'
      }
    };
  }

  changeNameHandler() {
    this.setState({ title: 'The changed title' })
  }

  render() {
    return (
      <div>
        <h1>Title - {this.state.title}</h1>
        <p>The content part</p>
        <ul>
          <li>Name: {this.state.person.jim}</li>
          <li>Age: {this.state.person.age}</li>
          <li>Location: {this.state.person.location}</li>
        </ul>
        <button onClick={this.changeNameHandler}>Change title</button>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

(1) 修改 state 的方法

  changeNameHandler() {
    this.setState({ title: 'The changed title' })
  }

(2) 方法绑定 this

在 constructor() 中,使用了 .bind(this)把该方法绑定在 this 上,这是因为 ES6 中,类方法并没有绑定在 this 上。后面可以使用 babel 插件,使用可以绑定 this 的方法。如果不需要在类中其他地方调用,可以不实用 .bind(this),比如生命周期的那些方法 componentDidMount() 等。

  constructor(props) {
    this.changeNameHandler = this.changeNameHandler.bind(this);
  }

当然可以把绑定 this 放在 JSX 中去做,比如:

        <button onClick={this.changeNameHandler.bind(this)}>Change title</button>

但是建议集中在 constructor 中管理,避免过于分散的 .bind(this) 散落在代码中。

(3) JSX 中调用方法

  render() {
    return (
      <div>
        {/*... */}
        <button onClick={this.changeNameHandler}>Change title</button>
      </div>
    );
  }
上一篇 下一篇

猜你喜欢

热点阅读