Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Last active September 23, 2015 12:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyraspopov/d03744d4b8d756a99d10 to your computer and use it in GitHub Desktop.
Save alexeyraspopov/d03744d4b8d756a99d10 to your computer and use it in GitHub Desktop.
Component, Container, External
'use strict';
var React = require('react'),
PureRenderMixin = require('react/lib/ReactComponentWithPureRenderMixin');
function Component(componentRender) {
return React.createClass({
mixins: [PureRenderMixin],
displayName: componentRender.name,
render() {
return componentRender(this.props);
}
});
}
module.exports = Component;
'use strict';
var React = require('react');
function subscribe(key, store, component) {
return store.subscribe(state => component.setState({[key]: state}));
}
function Container(stores, componentRender) {
var keys = Object.keys(stores);
return React.createClass({
displayName: componentRender.name,
getInitialState() {
return keys.reduce((state, key) => ({
[key]: stores[key].getState(),
...state
}), {});
},
componentDidMount() {
this.dispose = keys.map(key => subscribe(key, stores[key], this));
},
componentWillUnmount() {
this.dispose.forEach(fn => fn());
},
render() {
return componentRender(this.state, this.props);
}
});
}
module.exports = Container;
import React from 'react';
export default function External(componentRender, componentUpdate) {
return React.createClass({
displayName: componentRender.name,
shouldComponentUpdate() {
return false;
},
componentWillReceiveNewProps(newProps){
componentUpdate(newProps);
},
componentDidMount() {
componentRender(React.findDOMNode(this), this.props);
},
render() {
return (
<div className='external-content' />
);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment