Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type a higher order component with flow

I have the following HOC:

export default (keys: Array<string>) =>
  (WrappedComponent: React.Component<*, *, *>) => (props: Object): React.Element<*> => {
    if (hasNotYetLoadedProps(keys, props)) {
      return (
        <div
          style={{
            textAlign: 'center',
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            height: '100%',
          }}
        >
          <div>Loading</div>
        </div>
      )
    }
    return <WrappedComponent {... props} />
  }

that either render the original component or a loading indicator. With the actual flow type declaration I get this error:

React element `WrappedComponent`. Expected React component instead of React$Component 

in the last line. What would be the correct type of the incoming component?

like image 448
Andreas Köberle Avatar asked Jan 25 '17 21:01

Andreas Köberle


People also ask

What is the correct syntax for higher-order components?

Higher-order components or HOC is the advanced method of reusing the component functionality logic. It simply takes the original component and returns the enhanced component. Syntax: const EnhancedComponent = higherOrderComponent(OriginalComponent);

What is a higher-order component give us an example?

Higher-Order Components are not part of the React API. They are the pattern that emerges from React's compositional nature. The component transforms props into UI, and a higher-order component converts a component into another component. The examples of HOCs are Redux's connect and Relay's createContainer.

Can we use higher-order component in functional component?

Higher-Order Components enable us to apply functional programming principles on components by embracing composition. React Hooks, in contrast, transformed pure (in the sense of functional programming) function components to stateful/side-effect burdened beasts. Anyway, both have their right to exist.

What is a higher-order component?

A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React's compositional nature. Concretely, a higher-order component is a function that takes a component and returns a new component.


1 Answers

The type needs to ReactClass<any>

export default (keys: Array<string>) =>
  (WrappedComponent: ReactClass<any>) => (props: Object): React.Element<*> => {
    if (hasNotYetLoadedProps(keys, props)) {
      return (
        <div
          style={{
            textAlign: 'center',
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            height: '100%',
          }}
        >
          <div>Loading</div>
        </div>
      )
    }
    return <WrappedComponent {... props} />
  }
like image 119
Andreas Köberle Avatar answered Sep 27 '22 18:09

Andreas Köberle