求助 react 问题

新手上路,请多包涵

麻烦大佬帮忙看解释下这段react的代码的作用,另外,这个global是哪里来的?

const appCtx = React.createContext();
const { Provider: AppProvider, Consumer: AppConsumer } = appCtx;
const withAppComponent = Component => props => (
  <AppConsumer>
    {global => {
      let { global: propsGlobal, ecsRef, ...others } = props;
      return (
        <Component
          {...others}
          global={{ ...propsGlobal, ...global }}
        ></Component>
      );
    }}
  </AppConsumer>
export class WrapAppComponent extends Component {
  render() {
    let { id, component, global: propsGlobal, ecsRef, ...others } = this.props;
    return (
      <AppConsumer>
        {global =>
          React.createElement(
            component,
            {
              key: id,
              ref: ecsRef,
              ...others,
              global: { ...propsGlobal, ...global }
            },
            null
          )
        }
      </AppConsumer>
    );
  }
}
);
阅读 1.6k
2 个回答
新手上路,请多包涵

我的理解:
为全局使用context创建相关的provider和consumer
将需要增加相关provider参数的组件传入,返回的即为可以使用consumer获取对应参数的组件

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题