【问题标题】:React functional HOC component with Hooks, copying static methods使用 Hooks 反应功能性 HOC 组件,复制静态方法
【发布时间】:2019-04-28 18:25:21
【问题描述】:

我有一个使用钩子的功能性 HOC 组件。

我正在使用 Wix Native Navigation,并且我的大部分屏幕都使用静态方法。

但使用 HOC 时不会复制静态方法:

但是,当您将 HOC 应用于组件时,原始组件是 用容器组件包裹。这意味着新组件确实 没有任何原始组件的静态方法。

我正在尝试使用hoistNonReactStatic 但没有成功:

这是我的 HOC:

const WithOfflineAlertContainer = WrappedComponent => (props) => {
  const isConnected = useNetInfo();
  return hoistNonReactStatics(
    <Fragment>
      <WrappedComponent {...props} />
      {!isConnected && <OfflineAlert />}
    </Fragment>, WrappedComponent,
  );
};

这就是使用hocwix-react-native-navigation 的方法:

  Navigation.registerComponentWithRedux(screens.gallery, () => WithOfflineAlert(Gallery), Provider, store);

但它似乎不起作用,因为我没有看到 static options()wix native navigation 应用的样式

【问题讨论】:

    标签: higher-order-functions react-hooks wix-react-native-navigation


    【解决方案1】:

    所以我设法使用另一个帖子中的 answer 使其工作。

    这是工作版本

    const WithOfflineAlert = (Component) => {
      const WrappedComponent = (props) => {
        const isConnected = useNetInfo();
        return (
          <Fragment>
            <Component {...props} />
            {!isConnected && <OfflineAlert />}
          </Fragment>
        );
      };
      hoistNonReactStatics(WrappedComponent, Component);
      return WrappedComponent;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 2020-10-29
      • 2019-07-24
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2020-01-02
      相关资源
      最近更新 更多