-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.tsx
47 lines (41 loc) · 1.12 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use client";
import Form from "next/form";
import { useSearchParams } from "next/navigation";
import { use } from "react";
import fetchData from "../fetchData";
function cacheCompat<T>(fetcher: (query: string) => Promise<T>) {
const cache = new Map<string, Promise<T>>();
return (query: string) => {
const cachedOrNull = cache.get(query);
if (cachedOrNull) {
return cachedOrNull;
}
const promise = fetcher(query);
cache.set(query, promise);
return promise;
};
}
const fetchDataCached = cacheCompat(fetchData);
export default function React19() {
const searchParams = useSearchParams();
const query = searchParams.get("query") ?? "";
const data = use(fetchDataCached(query));
return (
<div>
<h1>React 19</h1>
<Form
action="/react-19"
className="border border-gray-300 rounded-md p-2 w-fit"
>
<input
type="text"
name="query"
className="outline-none"
defaultValue={query}
/>
<button type="submit">Search</button>
</Form>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}