I am using the recompose library and HoCs to pass down a bunch of props for my component. I'd like to know if there is a way, within the compose
to check for props before setting up an HoC. For example, say I have something like this:
export default compose(
withProps(props => ({
shouldUseWithMyHoc: someCondition() // true or false
})),
withState('someState', 'setSomeState', ''),
withHandlers({
doStuff: props => evt => {
// do stuff
}
}),
withMyHoc() // should only use if `shouldUseWithMyHoc` prop is true
)(MyComponent)
What can I do so that withMyHoc
is only used when shouldUseWithMyHoc
is set to true
?
I thought of perhaps doing a ternary, but that didn't go well and I had nothing to fallback on when the condition was false. It looked something like this:
export default compose (
...
(props) => {(
props.shouldUseWithMyHoc
? withMyHoc()
: null)}
)(MyComponent)
By using the ternary, I get the following error:
Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
Is there a way I can get this done? If this seems correct, then what am I doing wrong?