revite/src/redux/State.tsx

29 lines
685 B
TypeScript
Raw Normal View History

import { store } from ".";
import localForage from "localforage";
2021-06-18 14:25:33 -04:00
import { Provider } from "react-redux";
import { Children } from "../types/Preact";
import { useEffect, useState } from "preact/hooks";
async function loadState() {
const state = await localForage.getItem("state");
if (state) {
store.dispatch({ type: "__INIT", state });
}
}
interface Props {
2021-06-18 14:25:33 -04:00
children: Children;
}
export default function State(props: Props) {
const [loaded, setLoaded] = useState(false);
useEffect(() => {
loadState().then(() => setLoaded(true));
}, []);
if (!loaded) return null;
2021-06-18 14:25:33 -04:00
return <Provider store={store}>{props.children}</Provider>;
}