I am starting to develop a web application using React JS. I bought a theme from theme forest. In the theme, they are using compose like this in the component.
...Other code here
Login.propTypes = {
classes: PropTypes.shape({}).isRequired,
width: PropTypes.string.isRequired
};
export default compose(withWidth(), withStyles(themeStyles, { withTheme: true }))(Login);
As you can see their code is using the compose at the end when exporting the Component. I cannot modify their built-structure. What I like to do now is I like to use connect feature of the react as well.
Normally connect is used in the place of compose. Now, if I want to use connect to work with the state of the application, how can I use it together with compose ?
I hope this solves your problem.
compose
doesn't get rid of the pattern of passing a function to the result of a function call, but it reduces its use to one.Only one HOC, no gain from using compose:
Note that starting another function call immediately after a function call, which only recently came into fashion, is still there with
compose
.With two HOCs there is a gain from
compose
because the nesting of parens is less:This still can be hard to follow if you aren't used to a nameless function being called immediately after it's created. If you prefer you can give a name to it:
I prefer to use compose when there is more than one HOC, and to use it directly. I think cutting down on the nesting is useful but giving it a generic name like
enhance
is unnecessary.I was receiving the following error while using
compose
with Typescript:JSX element type 'App' does not have any construct or call signatures. TS2604
I had to add
ComponentType
in the following way to make it work: