Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

withHandlers redefining function breaks React.memo #11

@gastonmorixe

Description

@gastonmorixe

I have optimized withHandlers like this

const withHandlers = handlers => (props: any = {}) => {
  const realHandlers = React.useMemo(
    () => (typeof handlers === "function" ? handlers(props) : handlers),
    []
  )

  const [state] = React.useState(() => {
    const store: any = {}
    store.cachedProps = props
    store.handlers = Object.keys(realHandlers).reduce(
      (acc: {}, key) => ({
        ...acc,
        [key]: (...payload) => realHandlers[key](store.cachedProps)(...payload)
      }),
      {}
    )
    return store
  })

  state.cachedProps = props

  return { ...props, ...state.handlers }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions