revite/src/redux/State.tsx

29 lines
714 B
TypeScript
Raw Normal View History

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