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?
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);
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.
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.
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.
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} />
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With