Created
August 29, 2018 08:37
-
-
Save gladchinda/3b952257338163f50ae65db51e1b213c to your computer and use it in GitHub Desktop.
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 store from './reduxStore'; | |
import Profile from './components/Profile'; | |
class ProfileContainer extends Component { | |
state = this.getCurrentStateFromStore() | |
getCurrentStateFromStore() { | |
return { | |
profile: store.getState().user.profile, | |
loggedIn: store.getState().auth.loggedIn | |
} | |
} | |
updateStateFromStore = () => { | |
const currentState = this.getCurrentStateFromStore(); | |
if (this.state !== currentState) { | |
this.setState(currentState); | |
} | |
} | |
componentDidMount() { | |
this.unsubscribeStore = store.subscribe(this.updateStateFromStore); | |
} | |
componentWillUnmount() { | |
this.unsubscribeStore(); | |
} | |
render() { | |
const { loggedIn, profile } = this.state; | |
return ( | |
loggedIn | |
? <Profile profile={profile} /> | |
: <div>Please login to view profile.</div> | |
) | |
} | |
} | |
export default ProfileContainer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment