Defining function for withHandler in Recompose for React

Viewed 4249

All of the examples I look at, the function actually called in withHandlers seems to call a function from props, but I have no idea how that function is defined. Here is a small example from docs for humans.

compose(
  withState('count', 'setCount', 0),
  withHandlers({
    incrementCount: props => event => {
      event.preventDefault()
      props.setCount(props.count + 1)
    }
  })
)(ComponentToEnhance)

My understanding is this will create a HOC with "state" to track count. I would be able to increment the count via an action used the defined handler (e.g. onClick={incrementCount}).

My question is then, where is setCount actually defined.. I'm imaging something like

function setCount(i) {
    return i+1;
}

Since it is called from props, do you have to pass it in as props when your using the component? I'm confused why withState would need to know the state updater name, or how it is even related if that is the case.

Does it just define a function automatically for you that will replace the state param with whatever argument you pass it (facepalm if so..)

2 Answers
Related