Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Last active June 3, 2018 16:07
Show Gist options
  • Save jordanrios94/230343e66ccb3e8eadb5c207c712e059 to your computer and use it in GitHub Desktop.
Save jordanrios94/230343e66ccb3e8eadb5c207c712e059 to your computer and use it in GitHub Desktop.
HOC Scaffold Redux
import React, { Component } from 'react';
import { connect } from 'react-redux';
export default (ChildComponent) => {
class ComposedComponent extends Component {
// Our component just got rendered
componentDidMount() {
this.shouldNavigateAway();
}
// Our comonent just got updated with new props
componentDidUpdate() {
this.shouldNavigateAway();
}
shouldNavigateAway() {
if (!this.props.auth) {
console.log('I NEED TO LEAVE');
this.props.history.push('/');
}
}
// Methods above get injected into component
render() {
// Pass parent props to child
return <ChildComponent {...this.props} />;
};
}
const mapStateToProps = ({ auth }) => {
return { auth };
};
return connect(mapStateToProps)(ComposedComponent);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment