Skip to content

Instantly share code, notes, and snippets.

@danalloway
Created November 20, 2017 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danalloway/af983d6d086d2c5a7a21dfd97f5c2692 to your computer and use it in GitHub Desktop.
Save danalloway/af983d6d086d2c5a7a21dfd97f5c2692 to your computer and use it in GitHub Desktop.
redux-persist PersistGate component for Preact
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;
}
}
@factoidforrest
Copy link

what is onbeforelift?

@carlos-mtz-arenas
Copy link

For some reason, I had to change this.handlePersistorState to this.handlePersistorState.bind(this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment