Skip to content

Commit

Permalink
persist gate
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Nov 18, 2023
1 parent a94a6a5 commit 5001af1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions react.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./query/react.ts";
export * from "./store/react.ts";
export { Provider, useDispatch, useSelector } from "./deps.ts";
export type { TypedUseSelectorHook } from "./deps.ts";
30 changes: 30 additions & 0 deletions store/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AnyState } from "../types.ts";
import { LoaderItemOutput } from "./slice/loader.ts";
import { React, useSelector } from "../deps.ts";

interface PersistGateProps {
loader: LoaderItemOutput<AnyState, AnyState>;
children: React.ReactNode;
loading?: JSX.Element;
}

function Loading({ text }: { text: string }) {
return React.createElement("div", null, text);
}

export function PersistGate(
{ loader, loading = React.createElement(Loading), children }:
PersistGateProps,
) {
const loaderItem = useSelector(loader.select);

if (loaderItem.status === "error") {
return React.createElement("div", null, loaderItem.message);
}

if (loaderItem.status !== "success") {
return loading;
}

return React.createElement("div", null, children);
}

0 comments on commit 5001af1

Please sign in to comment.