Answer the question
In order to leave comments, you need to log in
How to wrap a component in HOC using compose?
I have a HOC (ThemeProvider from @material-ui/core) in which I wrap the component. I want to do this via compose (redux). But I can't understand how? Namely: how to pass userPalette to compose.
import {hot} from 'react-hot-loader/root';
interface IRoutesProps {
isTokenVerifying: boolean;
userPalette: IPalette;
}
const Routes = ({isTokenVerifying, userPalette}: IRoutesProps) => {
return (
<ThemeProvider theme={defaultMaterialTheme(userPalette)}>
<Switch>
....
</Switch>
</ThemeProvider>
);
};
const mapStateToProps = (state: RootState) => ({
isTokenVerifying: getPendingTokenVerify(state),
userPalette: getUserPalette(state),
});
export default hot(connect(mapStateToProps)(Routes));
const ThemeProviderContainer = ({ children, userPalette }: any) =>
<ThemeProvider theme={defaultMaterialTheme(userPalette)}>
{children}
</ThemeProvider>;
export default hot(
compose(connect(mapStateToProps), ThemeProviderContainer),
)(Routes);
Answer the question
In order to leave comments, you need to log in
The HOC should return a component, not an element to render.
In your case it will be something like this:
const withTheme = Component => ({ userPalette, ...props }) => (
<ThemeProvider theme={defaultMaterialTheme(userPalette)}>
<Component {...props} />
</ThemeProvider>
);
export default compose(
hot,
connect(mapStateToProps),
withTheme,
)(Routes);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question