Last active
June 3, 2018 16:07
-
-
Save jordanrios94/230343e66ccb3e8eadb5c207c712e059 to your computer and use it in GitHub Desktop.
HOC Scaffold Redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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