Skip to content

Commit 8d6ddad

Browse files
committed
small
1 parent b53c38c commit 8d6ddad

File tree

15 files changed

+208
-28
lines changed

15 files changed

+208
-28
lines changed

_fetch/rapidjs/.gitkeep

Whitespace-only changes.

_fetch/swr/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

_fetch/swr/combine-umi-request.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import useSWR from 'swr'
2+
import ReactDOM from "react-dom"
3+
import request from 'umi-request'
4+
5+
function App () {
6+
const { data, error } = useSWR('/mock/user.json', request)
7+
8+
if (error) return <div>failed to load</div>
9+
if (!data) return <div>loading...</div>
10+
11+
return <div>hello {data.data.name}!</div>
12+
}
13+
14+
ReactDOM.render(<App />, mountNode);
15+

_fetch/swr/combine-unfetch.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import useSWR from 'swr'
2+
import ReactDOM from "react-dom"
3+
import fetch from 'unfetch'
4+
5+
const fetcher = url => fetch(url).then(r => r.json())
6+
7+
function App () {
8+
const { data, error } = useSWR('/mock/user.json', fetcher)
9+
10+
if (error) return <div>failed to load</div>
11+
if (!data) return <div>loading...</div>
12+
return <div>hello {data.data.name}!</div>
13+
}
14+
15+
ReactDOM.render(<App />, mountNode);
16+

_fetch/swr/dependent-fetching.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import useSWR from 'swr'
2+
import ReactDOM from "react-dom"
3+
4+
const fetcher = url => fetch(url).then(r => r.json())
5+
6+
function App () {
7+
const { data: users } = useSWR('/mock/users.json', fetcher)
8+
const { data: user } = useSWR(() => '/mock/user.json?name=' + users.data[0].name, fetcher)
9+
10+
if (!user) {
11+
return <>loading...</>
12+
}
13+
14+
return <ul>
15+
{ users.data.map((item) => <li key={item.name} style={{
16+
color: item.name === user.data.name ? 'red' : 'blue'
17+
}}>{item.name}</li>) }
18+
</ul>
19+
}
20+
21+
ReactDOM.render(<App />, mountNode);

_fetch/swr/global-configuration.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import useSWR, { SWRConfig } from 'swr'
2+
import ReactDOM from 'react-dom'
3+
4+
function Dashboard () {
5+
const { data: user } = useSWR('/mock/users.json')
6+
7+
// const { data: user } = useSWR('/mock/users.json', { refreshInterval: 0 }) // --> don't refresh
8+
9+
if (!user) { return <>loading...</> }
10+
11+
return <ul>
12+
{ user.data.map((user) => <li key={user.name}>{user.name}</li>) }
13+
</ul>
14+
}
15+
16+
function App () {
17+
return (
18+
<SWRConfig
19+
value={{
20+
refreshInterval: 3000,
21+
// 窗口聚焦时自动重新验证
22+
revalidateOnFocus: true,
23+
fetcher: (...args) => fetch(...args).then(res => res.json())
24+
}}
25+
>
26+
<Dashboard />
27+
</SWRConfig>
28+
)
29+
}
30+
31+
ReactDOM.render(<App />, mountNode)

_fetch/swr/index.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

_fetch/swr/movie-graphql.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { request } from 'graphql-request'
2+
import useSWR from "swr";
3+
import ReactDOM from 'react-dom'
4+
5+
const API = 'https://api.graph.cool/simple/v1/movies'
6+
const fetcher = query => request(API, query)
7+
8+
function App () {
9+
const { data: resp, error } = useSWR(
10+
`{
11+
Movie(title: "Inception") {
12+
releaseDate
13+
actors {
14+
name
15+
}
16+
}
17+
}`,
18+
fetcher
19+
)
20+
if (!resp) return <div>loading...</div>
21+
return <div>{ JSON.stringify(resp) } </div>
22+
}
23+
24+
ReactDOM.render(<App />, mountNode)

_fetch/swr/simple-demo.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import useSWR from 'swr'
2+
import ReactDOM from "react-dom"
3+
4+
function App () {
5+
const { data, error } = useSWR('/mock/user.json', (url) => fetch(url).then(r => r.json()))
6+
7+
if (error) return <div>failed to load</div>
8+
if (!data) return <div>loading...</div>
9+
return <div>hello {data.data.name}!</div>
10+
}
11+
12+
ReactDOM.render(<App />, mountNode);

_fetch/swr/suspense-mode.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Suspense } from 'react'
2+
import useSWR from 'swr'
3+
import ReactDOM from 'react-dom'
4+
5+
import fetch from 'unfetch'
6+
7+
const fetcher = url => fetch(url).then(r => r.json())
8+
9+
10+
function Profile () {
11+
const { data } = useSWR('/mock/user.json', fetcher, { suspense: true })
12+
console.log(data)
13+
return <div>hello, { data.data.name }</div>
14+
}
15+
16+
function App () {
17+
return (
18+
<Suspense fallback={<div>loading...</div>}>
19+
<Profile/>
20+
</Suspense>
21+
)
22+
}
23+
24+
ReactDOM.render(<App/>, mountNode)

0 commit comments

Comments
 (0)