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

Getting weird warning when using withStateHandlers #757

Open
JulsRicketti opened this issue Nov 7, 2018 · 4 comments
Open

Getting weird warning when using withStateHandlers #757

JulsRicketti opened this issue Nov 7, 2018 · 4 comments

Comments

@JulsRicketti
Copy link

JulsRicketti commented Nov 7, 2018

I am using withStateHandlers to change a prop within a component. But I am getting an odd warning which I have no idea what it is and don't know if it may be warning me of undesired side effects or perhaps misuse of it.

This is the warning:

withStateHandlers(getContext(withStateHandlers(withProps(withStateHandlers(withProps(withState(MyComponent))))))).state: must be set to an object or null

Currently this is what my usage of withStateHandlers looks like:

export default compose(
  withStateHandlers(
    props => props.isButtonActive || false,
    {
      setButton: () => (isButtonActive) => {
        return {
          isButtonActive
        }
      }
    }
  )(MyComponent)

For context, within MyComponent I have a button that needs to be active after clicking it. However it gets it's original value from the server. For me to appropriately keep whether or not the button is active, I pass down the value I need as a prop (which is props.isButtonActive) and when the button is clicked, I call the setButton function. I don't know if relevant, but my component looks something like this:

export default MyComponent extends React.Component {
  render () {
    const { isButtonActive, setButton } = this.props
    return (
      <Button active={isButtonActive} onClick={() => setButton(!isButtonActive)}>My Button</Button>

    )
  }
}
@istarkov
Copy link
Contributor

istarkov commented Nov 8, 2018

Please provide example on codesandbox

@JulsRicketti
Copy link
Author

Update: I figured it out. It turns out that it wasn't affecting anything negatively besides the warning, however, the documentation for withStateHandlers is slightly incorrect or misleading to say the very least. See here

Basically in the API format it is described that the initialState must be either an object or a function that can return any type. However, from what I can tell here, the function must also return an object. So for me, what fixed this was simply returning an object instead of boolean when I was setting the initial state:

props => ({props.isButtonActive || false}),

So for the documentation to accurately informa that it should probably be changed to the following:

withStateHandlers(
  initialState: Object | (props: Object) => Object, // change here from any to Object
  stateUpdaters: {
    [key: string]: (state:Object, props:Object) => (...payload: any[]) => Object
  }
)

@istarkov
Copy link
Contributor

istarkov commented Nov 8, 2018

PR welcome

@JulsRicketti
Copy link
Author

Sounds good. Will try to remember to do this tonight!

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