Skip to content

Connect decorator #1

@mehcode

Description

@mehcode

@goatslacker I like what you have here with connect. But I miss the decorator version we used to have.

I suggest something like this:

function connectToStores(component) {
  if (component.contextTypes == null) component.contextTypes = {}
  if (component.contextTypes.flux == null) {
    component.contextTypes.flux = React.PropTypes.object.isRequired
  }

  let storeNames = []
  if (component.getStores != null) storeNames = component.getStores()
  if (component.stores != null) storeNames = component.stores
  if (component.store) storeNames = [component.store]

  return connect(component, (stores, flux) => {
    return {
      listenTo() {
        return storeNames.map((name) => flux.stores[name])
      },

      getProps() {
        if (component.getPropsFromStores != null) {
          return component.getPropsFromStores.apply(this, arguments)
        }

        const props = {}
        for (const storeName of storeNames) {
          props[storeName] = flux.stores[storeName].getState()
        }

        return props
      },
    }
  })
}

However I'm unsure how you'd want to introduce this into the library. Let me know your thoughts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions