Skip to content

Commit bc3e15c

Browse files
committed
add list result to SWRListItem
1 parent 029943b commit bc3e15c

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

examples/queries/pages/index.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@ import useSWRList from 'swr/list'
33
import useSWR from 'swr'
44
import { useState } from 'react'
55

6-
const SWRListItem = ({ originKey, fetcher }) => {
7-
const { data, error, isLoading, isValidating } = useSWR(originKey, fetcher)
8-
console.log(originKey, data, error, isLoading, isValidating)
9-
return <div>{data ? data : 'loading'}</div>
6+
const SWRListItem = ({ originKey, fetcher, listResult }) => {
7+
const { data, error, isLoading, isValidating, mutate } = useSWR(
8+
originKey,
9+
fetcher
10+
)
11+
console.log(
12+
originKey,
13+
data,
14+
error,
15+
isLoading,
16+
isValidating,
17+
listResult.data,
18+
listResult.error,
19+
listResult.isLoading,
20+
listResult.isValidating
21+
)
22+
return <div onClick={() => mutate()}>{data ? data : 'loading'}</div>
1023
}
1124

1225
export default function Index() {
@@ -15,7 +28,7 @@ export default function Index() {
1528
'/api/data?id=1',
1629
'/api/data?id=2'
1730
])
18-
const { data, result } = useSWRList(keys, fetch, {
31+
const { data, result, mutate } = useSWRList(keys, fetch, {
1932
SWRListItem,
2033
keepPreviousData: true
2134
})
@@ -29,7 +42,7 @@ export default function Index() {
2942
</button>
3043
<div>{result}</div>
3144
<h2>-----***----</h2>
32-
<div>
45+
<div onClick={() => mutate()}>
3346
{data ? (
3447
data.map(v => <div key={v.key}>{v.data ? v.data : v.error}</div>)
3548
) : (

list/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ export const list = (<Data, Error, Key extends Arguments = Arguments>(
117117
<SWRListItem
118118
key={key}
119119
originKey={_keys[index]}
120+
listResult={swr}
120121
fetcher={async () => {
121122
const data = await currentFetcher(_keys[index])
122-
fetch({ dedupe: true })
123+
swr.mutate()
123124
return data
124125
}}
125126
/>
@@ -131,8 +132,7 @@ export const list = (<Data, Error, Key extends Arguments = Arguments>(
131132
})
132133

133134
return {
134-
// eslint-disable-next-line react-hooks/exhaustive-deps
135-
result: useMemo(() => keys.map(listRender), [keys]),
135+
result: keys.map(listRender),
136136
mutate: (
137137
data: Data[] | Promise<Data[]> | MutatorCallback<Data[]> = () =>
138138
fetch({ dedupe: false }),

list/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface ItemComponentProps<
1919
> {
2020
originKey: Key
2121
fetcher: BareFetcher<Data>
22+
listResult: SWRListResponse<Data, Error>
2223
}
2324

2425
export interface SWRListConfiguration<

0 commit comments

Comments
 (0)