Created
November 20, 2017 21:12
-
-
Save danalloway/af983d6d086d2c5a7a21dfd97f5c2692 to your computer and use it in GitHub Desktop.
redux-persist PersistGate component for Preact
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 { h, Component } from 'preact'; | |
export default class PersistGate extends Component { | |
unsubscribe; | |
state = { | |
isBootstrapped: false | |
}; | |
handlePersistorState = () => { | |
const { onBeforeLift, persistor } = this.props; | |
const { bootstrapped } = persistor.getState(); | |
if (bootstrapped) { | |
onBeforeLift && onBeforeLift(); | |
this.setState({ isBootstrapped: true }); | |
this.unsubscribe && this.unsubscribe(); | |
} | |
}; | |
componentDidMount() { | |
const { persistor } = this.props; | |
this.unsubscribe = persistor.subscribe(this.handlePersistorState); | |
} | |
componentWillUnmount() { | |
this.unsubscribe && this.unsubscribe(); | |
} | |
render({ children, loading = null, persistor }, { isBootstrapped }) { | |
return isBootstrapped ? children[0] : loading; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason, I had to change
this.handlePersistorState
tothis.handlePersistorState.bind(this)