Skip to content

Instantly share code, notes, and snippets.

@moimikey
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moimikey/4c0daa616a1fc522b3ef to your computer and use it in GitHub Desktop.
Save moimikey/4c0daa616a1fc522b3ef to your computer and use it in GitHub Desktop.
Idea for React sync with store via HOC

Did I steal this idea? Did I read this briefly once and all of a sudden it hit me? Maybe there's a Flux implementation that does something like this? Perhaps I should understand Flux a little more? Seems legit.

// wrapper

let syncWith = function syncWith(store) {
  return React.createClass({
    getInitialState() {
      return store.getState();
    },
    componentDidMount() {
      return store.listen(this.onChange);
    },
    componentWillUnmount() {
      return store.unlisten(this.onChange);
    },
    render() {
      return(
        <{this.props.elementType} className={this.props.className}>
          {this.props.children}
        </{this.props.elementType}>
      )
    }
  })
};

// somewhere else...

var SyncWith     = require(...);
var ProductStore = require(...);
var ...

<SyncWith store={ProductStore}>
  <Component {...this.props} className="Products" elementType="div" />
</SyncWith>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment