Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Update withStateHandlers initialState when receiving new props #695

Open
rkrishnan8594 opened this issue Jun 27, 2018 · 1 comment
Open

Comments

@rkrishnan8594
Copy link

I recently ran into an issue in a project with @lamflam where we wanted to recalculate the initialState on withStateHandlers when the component receives new props. Here is the solution that we came up with, which makes use of getDerivedStateFromProps and the existing withStateHandlers utility:

import React, { PureComponent } from 'react';
import { withStateHandlers as withRecomposeStateHandlers } from 'recompose';

export const withStateHandlers = (initialState, stateUpdaters) => WrappedComponent => {
    class WithStateHandlers extends PureComponent {
        static getDerivedStateFromProps(props, state) {
            return initialState(props, state);
        }

        constructor(props) {
            super(props);
            this.state = {};
        }

        render() {
            return React.createElement(WrappedComponent, { ...this.props, ...this.state });
        }
    }

    return withRecomposeStateHandlers(props => initialState(props, {}), stateUpdaters)(WithStateHandlers);
};

We've read a few of the GitHub issues where it's been explained why this isn't a part of withStateHandlers or isn't its own recompose utility, but I thought I'd post it here and see if this solution is helpful to anyone else. Also more than happy to clean this up and turn it into a PR if it would make sense to include this with recompose.

@derekstavis
Copy link

derekstavis commented Oct 2, 2018

This is as simple as adding

  if (typeof initialState === 'function') {
    WithStateHandlers.getDerivedStateFromProps = initialState
  }

to withStateHandlers HOC.

I've copy/pasted the code from recompose source code into my codebase and did this change, now my use case is covered by this component.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants