反应高阶组件函数参数

时间:2021-01-29 12:06:08

标签: javascript reactjs

在传递第二个参数 selectData 时,我不明白 react docs 中的 HOC 示例。该函数是如何传递和使用的,就像 selectData 发生的一般流程一样?

const CommentListWithSubscription = withSubscription(
  CommentList,
  (DataSource) => DataSource.getComments()
);

// This function takes a component...
function withSubscription(WrappedComponent, selectData) {
  // ...and returns another component...
  return class extends React.Component {
      constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
        this.state = {
          data: selectData(DataSource, props)
        };
      }

1 个答案:

答案 0 :(得分:0)

我终于明白了。 DataSource 是一个全局的。 调用 SelectData 会触发箭头函数提供的回调,即 DataSource.getComments()。