You can conditionally load a wrapping component in React by using a Higher Order Component (HOC) that accepts a component as a parameter and returns a new component that wraps it with additional functionality.
Here's an example of an HOC that conditionally renders a wrapping component based on a boolean prop called "condition":
352 chars16 lines
In this example, the withWrapper
HOC takes in two parameters: the WrappedComponent
that we want to render conditionally and the WrapperComponent
that we want to use as the wrapper. The HOC returns a new component that renders the WrappedComponent
wrapped in the WrapperComponent
only if the condition
prop is true.
To use this HOC, you can simply pass in your component and conditional prop like this:
583 chars26 lines
In this example, we defined our MyComponent
and MyWrapper
components, and then wrapped our MyComponent
with the MyWrapper
by using the withWrapper
HOC. We can pass condition={true}
to tell the HOC to wrap the component, or omit the prop to render the MyComponent
component without wrapping it.
gistlibby LogSnag