Skip to content

Instantly share code, notes, and snippets.

@mattd
Last active December 6, 2016 00:53
Show Gist options
  • Save mattd/630bce9f95558d91ea052a8423c6772c to your computer and use it in GitHub Desktop.
Save mattd/630bce9f95558d91ea052a8423c6772c to your computer and use it in GitHub Desktop.
import React from 'react';
const getDisplayName = (Component) => {
return Component.displayName || Component.name || 'Component'
};
export const makeController = (willMount, willUnmount) => {
return (Component) => {
const noop = () => {};
class Controller extends React.Component {
componentWillMount() {
willMount ? willMount(this.props) : noop();
}
componentWillUnmount() {
willUnmount ? willUnmount(this.props) : noop();
}
render() {
return (
<Component {...this.props} />
);
}
}
Controller.displayName = `Controller(${getDisplayName(Component)})`;
return Controller;
};
};
import React from 'react';
import { connect } from 'react-redux';
import { makeController } from '../controllers';
import { getTermsPath } from '../services/terms';
import {
subscribeToTerms,
unsubscribeFromTerms,
flushTerms
} from '../actions/creators/terms';
import { Feature } from '../components/containers';
const mapStateToProps = (state) => {
return {
db: state.db,
uid: state.profile.uid
};
};
const willMount = ({
db,
uid,
dispatch
}) => {
if (!db[getTermsPath(uid)]) {
dispatch(subscribeToTerms(uid));
}
};
const willUnmount = ({
uid,
dispatch
}) => {
if (uid) dispatch(unsubscribeFromTerms(uid));
dispatch(flushTerms());
};
const Terms = () => {
return (
<Feature title="Terms" />
);
};
export default connect(mapStateToProps)(
makeController(willMount, willUnmount)(Terms)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment