Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Last active March 22, 2023 00:19
Show Gist options
  • Save jordanrios94/f9ec406a5ef4a5cb25f0fc7796441c10 to your computer and use it in GitHub Desktop.
Save jordanrios94/f9ec406a5ef4a5cb25f0fc7796441c10 to your computer and use it in GitHub Desktop.
HOC Scaffold
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 component just got updated
componentDidUpdate() {
this.shouldNavigateAway();
}
shouldNavigateAway() {
if (!this.props.auth) {
this.props.history.push('/');
}
}
function mapStateToProp(state) {
return { auth: state.auth }
}
render() {
return <ChildComponent {...this.props} />;
};
}
return connect(mapStateToProps)(ComposedComponent);
};
// Imagine we are in CommentBox.js
/*
import requireAuth from 'components/requireAuth'
class CommentBox {}
export default requireAuth(CommentBox);
*/
// Imagine we are in App.js
/*
import CommentBox from './CommentBox';
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment