teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

情報の追加

2021/10/23 12:58

投稿

tanana_m
tanana_m

スコア28

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  理解できないのはソースの以下の箇所です。
2
2
 
3
- https://github.com/vercel/swr/blob/master/src/types.ts#L6-L32
3
+ [https://github.com/vercel/swr/blob/master/src/types.ts#L6-L32](https://github.com/vercel/swr/blob/master/src/types.ts#L6-L32)
4
4
 
5
5
  ```typescript
6
6
  export type Fetcher<Data = unknown, SWRKey extends Key = Key> =
@@ -37,4 +37,42 @@
37
37
  なぜneverの分岐に入らずこのような動作になるのでしょうか。
38
38
 
39
39
  ご教示いただけると大変助かります。
40
- よろしくお願いいたします。
40
+ よろしくお願いいたします。
41
+
42
+ ## useSWRの定義
43
+
44
+ 追っていくと以下にあたります。
45
+ [https://github.com/vercel/swr/blob/da33f52633ef5bb357e074de8499c76661e23c78/src/use-swr.ts#L512](https://github.com/vercel/swr/blob/da33f52633ef5bb357e074de8499c76661e23c78/src/use-swr.ts#L512)
46
+
47
+ `withArgs`自体の定義は以下です。
48
+
49
+ [https://github.com/vercel/swr/blob/da33f52633ef5bb357e074de8499c76661e23c78/src/utils/resolve-args.ts#L11-L36](https://github.com/vercel/swr/blob/da33f52633ef5bb357e074de8499c76661e23c78/src/utils/resolve-args.ts#L11-L36)
50
+
51
+ ```typescript
52
+ export const withArgs = <SWRType>(hook: any) => {
53
+ return (((...args: any) => {
54
+ // Normalize arguments.
55
+ const [key, fn, _config] = normalize<any, any>(args)
56
+
57
+ // Get the default and inherited configuration.
58
+ const fallbackConfig = mergeObjects(
59
+ defaultConfig,
60
+ useContext(SWRConfigContext)
61
+ )
62
+
63
+ // Merge configurations.
64
+ const config = mergeConfigs(fallbackConfig, _config)
65
+
66
+ // Apply middleware
67
+ let next = hook
68
+ const { use } = config
69
+ if (use) {
70
+ for (let i = use.length; i-- > 0; ) {
71
+ next = use[i](next)
72
+ }
73
+ }
74
+
75
+ return next(key, fn || config.fetcher, config)
76
+ }) as unknown) as SWRType
77
+ }
78
+ ```