react-compose-state icon indicating copy to clipboard operation
react-compose-state copied to clipboard

How to use react-compose-state with connect() and withRouter()?

Open ldco2016 opened this issue 6 years ago • 1 comments

Hello dai-shi,

I would like to implement your react-compose-state importing the following:

import PropTypes from "prop-types";
import { composeWithState } from "react-compose-state";

What I am not clear on is how to wire it up for my functional component when I already have this:

export default connect(
  mapStateToProps,
  actions
)(withRouter(SurveyFormReview));

How do I refactor the above to include something like this example:

export default composeWithState({ text: '' })(TextBox);

ldco2016 avatar Jun 05 '19 17:06 ldco2016

You could do it like this:

export default connect(
  mapStateToProps,
  actions
)(withRouter(composeWithState({ text: '' })(SurveyFormReview)));

But this becomes a bit ugly. You might want to use recompose in such a complex scenario.

export default compose(
  connect(
    mapStateToProps,
    actions
  ),
  withRouter,
  withState('text', 'setText', '')
)(SurveyFormReview);

Note: Those are only applicable to below React 16.8 which has hooks.

dai-shi avatar Jun 05 '19 22:06 dai-shi