File tree Expand file tree Collapse file tree 4 files changed +31
-35
lines changed Expand file tree Collapse file tree 4 files changed +31
-35
lines changed Original file line number Diff line number Diff line change 7
7
"start" : " next start"
8
8
},
9
9
"dependencies" : {
10
- "isomorphic-unfetch" : " 3.0.0" ,
11
10
"next" : " latest" ,
12
11
"react" : " ^16.8.6" ,
13
- "react-dom" : " ^16.8.6"
12
+ "react-dom" : " ^16.8.6" ,
13
+ "swr" : " ^0.1.18"
14
14
},
15
15
"license" : " ISC"
16
16
}
Original file line number Diff line number Diff line change 1
1
// Fake users data
2
- const users = [
3
- {
4
- id : 1 ,
5
- } ,
6
- { id : 2 } ,
7
- { id : 3 } ,
8
- ]
2
+ const users = [ { id : 1 } , { id : 2 } , { id : 3 } ]
9
3
10
4
export default ( req , res ) => {
11
5
// Get data from your database
Original file line number Diff line number Diff line change 1
- import fetch from 'isomorphic-unfetch '
1
+ import useSwr from 'swr '
2
2
import Link from 'next/link'
3
3
4
- const Index = ( { users } ) => (
5
- < ul >
6
- { users . map ( user => (
7
- < li key = { user . id } >
8
- < Link href = "/user/[id]" as = { `/user/${ user . id } ` } >
9
- < a > { `User ${ user . id } ` } </ a >
10
- </ Link >
11
- </ li >
12
- ) ) }
13
- </ ul >
14
- )
4
+ const fetcher = url => fetch ( url ) . then ( res => res . json ( ) )
15
5
16
- Index . getInitialProps = async ( ) => {
17
- const response = await fetch ( 'http://localhost:3000/api/users' )
18
- const users = await response . json ( )
6
+ export default function Index ( ) {
7
+ const { data, error } = useSwr ( '/api/users' , fetcher )
19
8
20
- return { users }
21
- }
9
+ if ( error ) return < div > Failed to load users </ div >
10
+ if ( ! data ) return < div > Loading... </ div >
22
11
23
- export default Index
12
+ return (
13
+ < ul >
14
+ { data . map ( user => (
15
+ < li key = { user . id } >
16
+ < Link href = "/user/[id]" as = { `/user/${ user . id } ` } >
17
+ < a > { `User ${ user . id } ` } </ a >
18
+ </ Link >
19
+ </ li >
20
+ ) ) }
21
+ </ ul >
22
+ )
23
+ }
Original file line number Diff line number Diff line change 1
- import fetch from 'isomorphic-unfetch'
1
+ import { useRouter } from 'next/router'
2
+ import useSwr from 'swr'
2
3
3
- const User = ( { user } ) => < div > { user . name } </ div >
4
+ const fetcher = url => fetch ( url ) . then ( res => res . json ( ) )
4
5
5
- User . getInitialProps = async ( { query : { id } } , res ) => {
6
- const response = await fetch ( `http://localhost:3000/api/user/ ${ id } ` )
7
- const user = await response . json ( )
6
+ export default function User ( ) {
7
+ const router = useRouter ( )
8
+ const { data , error } = useSwr ( `/api/user/ ${ router . query . id } ` , fetcher )
8
9
9
- return { user }
10
- }
10
+ if ( error ) return < div > Failed to load user </ div >
11
+ if ( ! data ) return < div > Loading... </ div >
11
12
12
- export default User
13
+ return < div > { data . name } </ div >
14
+ }
You can’t perform that action at this time.
0 commit comments