I'm trying to render a HOC with react but I cannot figure it out how to make it works. So I have one HOC that is working perfect with react-navigation. My idea is to show a component that the render wraps a HOC. I am trying to do sth like this:
render() {
return (
<View style={viewStyle}>
{CheckLogin(Login)}
</View>
);
}
this CheckLogin is the HOC and Login is the component itself. The result is that React is not complaining but is in blank. Any idea how to render a HOC invoking the Component itself??
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.
We have created a new component called HOC which returns the <WrappedComponent/> from its render function. While this actually adds no functionality, it depicts the common pattern that every HOC function will follow. We can invoke the HOC as follows: const SimpleHOC = higherOrderComponent(MyComponent);
The React community is moving away from HOC (higher order components) in favor of render prop components (RPC). For the most part, HOC and render prop components solve the same problem. However, render prop components provide are gaining popularity because they are more declarative and flexible than an HOC.
You are just calling the HOC as a function inside the JSX, instead you need to use </>
in order to render it.
// Apply your HOC
const EnhancedComponent = CheckLogin(Login);
class MyComponent extends Component {
render() {
return (
<View style={viewStyle}>
<EnhancedComponent />
</View>
);
}
}
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