Skip to content

Update connect to make it more extensible #285

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

Closed
wants to merge 1 commit into from

Conversation

mhodgson
Copy link

I've attempted to refactor connect to provide additional (optional) exports that would allow the user to easily create a custom Connect class. This allows the user to override specific parts of Connect while maintaining all the existing caching and update logic. Here is an example of usage:

import { Connect, createConnect } from 'react-redux/components/connect'

class MyConnect extends Connect {
  handleChange() {
    if (!this.unsubscribe) {
      return
    }

    const prevStoreState = this.state.storeState
    const storeState = this.store.getState()

    if (!this.pure || prevStoreState !== storeState) {
      this.hasStoreStateChanged = true
      return new Promise((resolve) => { this.setState({ storeState }, resolve) })
    }
  }
}

export default createConnect(MyConnect)

In this example I've overridden the handleChange method to return a Promise that resolves when the changes have been flushed to the DOM.

You then just use this custom connect method to decorate your components instead of the default one from react-redux.

All but one of the tests are passing, but I'm interested in getting feedback on this before working on it further.

@gaearon
Copy link
Contributor

gaearon commented Feb 11, 2016

Inheritance is generally problematic, and especially so when mixed with a functional paradigm. React components should not create class hierarchies, and even for this use case we don’t want to do that.

To solve #269, I would much prefer to extract the core logic from connect() so it can become a simple wrapper that is easy to write on your own with custom logic.

@gaearon gaearon closed this Feb 11, 2016
@mhodgson
Copy link
Author

@gaearon thanks for the quick feedback. I'm really interested in getting something that works for this, but it seems you have pretty specific ideas about how it should work. Would you mind giving a little bit more guidance on how you'd like to see this structured? Happy to put the work in to get to something useful. I'm struggling to see how to get a clean API here without inheritance when so many lifecycle methods are involved. We could double wrap the component but that seems even worse to me.

@gaearon
Copy link
Contributor

gaearon commented Feb 12, 2016

Something like subscribeComponentToStateChanges(store, callback, mapStateToProps, mapDispatchToProps, mergeProps, options)?

@slorber
Copy link
Contributor

slorber commented Feb 13, 2016

@mhodgson yes i'd also prefer to avoid inheritence but rather use composition / strategy pattern, where the default strategy remains compatible with current behavior.

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

Successfully merging this pull request may close these issues.

3 participants