File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed
Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -39,8 +39,31 @@ export type Hook<T> = {
3939const SWR_PREFIX = "CourseMate::useSWR::" ;
4040// todo: consider using useSWR Hook from external instead.
4141/**
42- use static function instead of inline arrow function
43- to prevent unnecessary useCallback calls.
42+ DANGER: `schema` や `fetcher` は *絶対に* inline で書いてはいけない。
43+ inline で書くと、無限描画ループに陥る。
44+ 例:
45+ ```javascript
46+ // BAD
47+ function Component() {
48+ const data = useCustomizedSWR(
49+ "myUniqueKey",
50+ () => fetch("/path"), // ✘
51+ z.array(z.number()), // ✘
52+ );
53+ }
54+ // GOOD
55+ const fetcher = () => fetch("/path");
56+ const schema = z.array(z.number());
57+
58+ function Component() {
59+ const data = useCustomizedSWR(
60+ "myUniqueKey",
61+ fetcher, // ◯
62+ schema, // ◯
63+ );
64+ }
65+ ```
66+
4467 cacheKey **MUST** be unique in all the codebase, otherwise the cache will interfere each other.
4568 (I recommend using URL Path, friend's name + unique prefix, or randomly generate static string.)
4669 **/
Original file line number Diff line number Diff line change @@ -51,6 +51,23 @@ async function safeReadData<T>(
5151}
5252
5353// TODO: refactor this to look better.
54+ /**
55+ DANGER: `schema` は *絶対に* inline で書いてはいけない。
56+ inline で書くと、無限描画ループに陥る。
57+ 例:
58+ ```javascript
59+ // BAD
60+ function Component() {
61+ const data = useAuthorizedData("/path", z.array(z.number()));
62+ }
63+ // GOOD
64+ const schema = z.array(z.number());
65+
66+ function Component() {
67+ const data = useAuthorizedData("/path", schema);
68+ }
69+ ```
70+ */
5471export function useAuthorizedData < T > ( url : string , schema : Zod . Schema < T > ) {
5572 const [ data , setData ] = useState < T | null > ( null ) ;
5673 const [ loading , setLoading ] = useState < boolean > ( false ) ;
You can’t perform that action at this time.
0 commit comments