launchdarkly / react-client-sdk

LaunchDarkly Client-side SDK for React.js
Other
85 stars 68 forks source link

HOC should hoist statics #68

Closed spacesuitdiver closed 3 years ago

spacesuitdiver commented 3 years ago

This provider HOC should hoist statics so users using something like static getInitialProps() {} can use the HOC properly.

https://github.com/launchdarkly/react-client-sdk/blob/2f0fad5d9b2c2f8561aa7ccab74415bfb17b772e/src/withLDProvider.tsx#L25

Here's the patch I did in our project for now:

const withLDProvider = (config) => (WrappedComponent) => {
  const EnhancedComponent = (props) => (
    <LDProvider {...config}>
      <WrappedComponent {...props} />
    </LDProvider>
  );

  hoistNonReactStatics(EnhancedComponent, WrappedComponent);
  return EnhancedComponent;
};

This is documented here for more information (should also consider using displayName): https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over

bwoskow-ld commented 3 years ago

Hi @spacesuitdiver,

Thank you for the suggestion. I'm adding it to our SDK backlog. Given that you've already got a patch, would you be willing to submit a pull request with the patched changes? That'll help speed up the time to release the change publicly.

Thanks, @bwoskow-ld

Filed internally as 107066.

bprymicz commented 3 years ago

Any update on this? We're experiencing a similar issue with not being able to use getInitialProps with the HOC.

bwoskow-ld commented 3 years ago

No, we have no update at this time.

bwoskow-ld commented 3 years ago

This is available in SDK version 2.23.0.