Skip to content

Commit 2c6addb

Browse files
committed
feat: improvements for loader to sync with url changes
1 parent b6837cd commit 2c6addb

File tree

15 files changed

+1894
-1532
lines changed

15 files changed

+1894
-1532
lines changed
Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import React, { Fragment } from "react";
1+
import React from "react";
22
import { useLoadingState, useQueryStringState } from "../../../../src/brouther/brouther";
33
import { Form, jsonResponse, LoaderProps, useDataLoader } from "../../exports";
44

55
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
66

77
export const loader = async (args: LoaderProps) => {
88
const cep = (args.queryString as any).cep || "30260-070";
9+
const now = performance.now();
910
await sleep(4000);
10-
console.log("JSON ->", cep);
11+
console.log("LOADER JSON ->", cep, performance.now() - now);
1112
return jsonResponse({ cep });
1213
};
1314

14-
const debounce = (fn: Function, ms = 0) => {
15+
const debounce = (fn: Function, ms: number = 0) => {
1516
let timeoutId: NodeJS.Timeout;
1617
return function (...args: any[]) {
1718
clearTimeout(timeoutId);
@@ -21,30 +22,24 @@ const debounce = (fn: Function, ms = 0) => {
2122

2223
export default function LoaderPage() {
2324
const data = useDataLoader<typeof loader>();
24-
const loading = useLoadingState();
25-
const [q, setQ] = useQueryStringState();
25+
const [q, setQ] = useQueryStringState<any>();
26+
const loading = useLoadingState()
2627
return (
2728
<div className="h-full flex flex-col gap-6 items-center justify-center w-full">
28-
{loading ? (
29-
"Loading..."
30-
) : (
31-
<Fragment>
32-
{" "}
33-
<Form method="get" encType="json" className="flex gap-4">
34-
<input name="cep" className="bg-white border p-1" />
35-
<button type="submit">Test</button>
36-
</Form>
37-
<input
38-
className="bg-white border p-1"
39-
placeholder="Debounce test"
40-
onChange={debounce((e: React.ChangeEvent<HTMLInputElement>) => {
41-
setQ({ query: e.target.value });
42-
console.log("After 2 seconds");
43-
}, 1200)}
44-
/>
45-
<div>{JSON.stringify(data, null, 4)}</div>
46-
</Fragment>
47-
)}
29+
<p>{loading ? "Loading..." : ""}</p>
30+
<Form method="get" encType="json" className="flex gap-4">
31+
<input name="cep" className="bg-white border p-1" />
32+
<button type="submit">Test</button>
33+
</Form>
34+
<input
35+
className="bg-white border p-1"
36+
placeholder="Debounce test"
37+
onChange={debounce((e: React.ChangeEvent<HTMLInputElement>) => {
38+
setQ((prev: any) => ({ ...prev, query: e.target.value }));
39+
console.log("After 2 seconds");
40+
}, 1200)}
41+
/>
42+
<div>{JSON.stringify({ data, q }, null, 4)}</div>
4843
</div>
4944
);
5045
}

0 commit comments

Comments
 (0)