高阶组件

高阶组件(React 官网翻译)

高阶组件就是函数将一个组件作为参数,然后再返回一个组件

const EnhancedComponent = higherOrderComponent(WrappedComponent);

一个组件是 React 复用代码的最小单元,但是你会发现一些模式并不能直接被传统的组件套用。

如果,加入一个需要外部数据源去渲染一个 list 的 comments 的 CommentList 组件,如下:

class CommentList extends React.Component {
  constructor() {
    super();
    this.handleChange = this.handleChange.bind(this);
    this.state = {
      comment: DataSource.getComments()
    }
  }
  componentDidMount() {
    DataSource.addChangeListener(this.handleChange);
  }
  componentWillUnMount() {
    DataSource.removeChangeListener(this.handleChange);
  }
  handleChange() {
    this.setState({
      comments: DataSource.getComments()
    })
  }
  render() {
    return (
      <div>
      {this.state.comments.map((comment) => (
       <Comment comment={comment} key={comment.id} />
       ))}
      </div>
    )
  }
}

接着我们需要写一个简单的 blog 组件,用的同样的模式

class BlogPost extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.state = {
      blogPost: DataSource.getBlogPost(props.id)
    }
  }
  componentDidMount() {
    DataSource.addChangeListener(this.handleChange);
  }
  componentWillUnmount() {
    DataSource.removeChangeListener(this.handleChange);
  }
  handleChange() {
    this.setState({
      blogPost: DataSource.getBlogPost(this.props.id)
    })
  }
  render() {
    return <TextBlock text={this.state.blogPost} />
  }
}

CommentList 和 BlogPost 并不相同,他们调用的是 DataSource 的不同的方法,渲染出来的结果也不相同,但是它们仍然有一些共性:

  • mount 时,新增一个 change listener 到 DataSource
  • 在 listener 中,不论数据如何变动都会调用 setState
  • unmount 时,移除 change listener

你可以想象在大型的 App,通过 DateSource 的数据渲染,然后调用 setState 这种相同的模式将会发生的频繁,我们想要在一个单独的地方抽象出这个逻辑然后在多个组件中共享,这就是高阶组件做的。

我们可以写一个函数创建组件,比如 CommentList 和 BlogPost,subscribe to DataSource。这个函数将会接收一个子组件作为参数,子组件接收订阅数据(subscribed data)作为 prop,让我们调用函数 withSubscription

const CommentListWithSubscription = withSubscription (
  CommentList,
  (DataSource) => DataSource.getComments()
)
const BlogPostWithSubscription = withSubscription (
    BlogPost,
    (DataSource,prop) => DataSource.getBlogPost(props.id)
)

第一个参数是被包裹的组件,第二个参数取回我们需要的数据,given a DataSource and the current props

当 CommentListWithSubscription 和 BlogPostWithSubscription 被渲染,CommentList 和 BlogPost 将被传递一个 data prop,这个 prop 里面包含了 DataSource 里的数据

function withSubscription(WrappedComponent,selectData) {
  return class extends React.Component {
    constructor(props) {
      super(props);
      this.handleChange = this.handleChange.bind(this);
      this.setState({ data: selectData(DataSource, props)})
    }
  }
  componentDidMount() {
    DataSource.addChangeListener(this.handleChange);
  }
  componentWillUnmount() {
    DataSource.removeChangeListener
  }
  hangdleChange() {
    this.setState({
      data: selectData(DataSource, this.props)
    })
  }
  render() {
    return <WrappedComponent data={this.state.data} {...this.props}>
  }
}

请注意,HOC不会修改输入组件,也不会使用继承来复制其行为。 相反,HOC通过将原始组件包装在容器组件中来组成原始组件。 HOC是具有零副作用的纯功能。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • In this article we will discuss how to use Higher Order C...
    人头原子弹阅读 688评论 0 0
  • 高阶组件是对既有组件进行包装,以增强既有组件的功能。其核心实现是一个无状态组件(函数),接收另一个组件作为参数,然...
    柏丘君阅读 3,213评论 0 6
  • 本文章将从四个方面讲解高阶组件 什么是高阶组件? 高阶组件是为了解决什么问题? 常见用法 与父组件的区别 什么是高...
    rangel阅读 3,392评论 0 3
  • 在目前的前端社区,『推崇组合,不推荐继承(prefer composition than inheritance)...
    Wenliang阅读 78,057评论 16 124
  • 不知从何时起 我与“拖延”二字形影不离 读书时 听着铃声走进课室 要不就是迟到几分钟 实验区的特色就是有个“论文周...
    FionaFung阅读 175评论 0 0

友情链接更多精彩内容