在这个类继承中使用了什么ES6 / React魔法?

时间:2015-07-06 20:29:22

标签: javascript class inheritance reactjs ecmascript-6

通过Redux的TodoMVC示例我发现了这个类继承的不寻常的例子。类Header可能按照惯例扩展React.Component(和所有React组件一样,对吧?),但是代码中没有明确说明。我错过了什么?这段代码是如何工作的?

import React, { PropTypes } from 'react';
import TodoTextInput from './TodoTextInput';

export default class Header {
  static propTypes = {
    addTodo: PropTypes.func.isRequired
  };

  handleSave(text) {
    if (text.length !== 0) {
      this.props.addTodo(text);
    }
  }

  render() {
    return (
      <header className='header'>
          <h1>todos</h1>
          <TodoTextInput newTodo={true}
                         onSave={::this.handleSave}
                         placeholder='What needs to be done?' />
      </header>
    );
  }
}

1 个答案:

答案 0 :(得分:5)

如果您不需要ReactComponentsetState()forceUpdate())定义的方法,则不必继承该方法。

因此,它不是类继承或魔术的一个例子,因为这里都没有发生:)