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

What ES6/React magic is used in this class inheritance?

本文关键字:什么 ES6 魔术 React 继承      更新时间:2023-09-26

通过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>
    );
  }
}

如果不需要ReactComponent(setState()forceUpdate()(定义的方法,则不必从中继承。

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