Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect decorator #1

Open
mehcode opened this issue Dec 23, 2015 · 1 comment
Open

Connect decorator #1

mehcode opened this issue Dec 23, 2015 · 1 comment

Comments

@mehcode
Copy link

mehcode commented Dec 23, 2015

@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.

@goatslacker
Copy link
Member

I didn't like the static properties stuff we used to have because it's too easy to abuse the container component and make your component not reusable by tying the state logic inside.

Same goes for the decorator. Making a decorator version out of this though seems pretty easy with some currying.

prcolaco pushed a commit to decentrawise/alt-react that referenced this issue Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants