Skip to content

Instantly share code, notes, and snippets.

@brigand
Last active April 18, 2017 21:24
Show Gist options
  • Save brigand/02bbab1892392a0cbb22138cd08609e3 to your computer and use it in GitHub Desktop.
Save brigand/02bbab1892392a0cbb22138cd08609e3 to your computer and use it in GitHub Desktop.
const fetchesDataOnMount = (callback) => (C) => class DataFetcher extends React.Component {
constructor(props) {
super(props);
if (__SERVER__) callback(this.props);
}
componentDidMount() {
if (!__SERVER__) callback(this.props);
}
render() {
return <C {...this.props} />;
}
};
class MyComponent extends React.Component {
// ...
componentWillReceiveProps(nextProps) {
if (nextProps.id !== this.props.id) fetchData(nextProps);
}
};
const fetchData = (props) => props.fetchData(props.id);
let Wrapped = C;
Wrapped = connect(...)(C);
Wrapped = fetchesDataOnMount(fetchData)(C);
export default Wrapped;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment