diff --git a/examples/api-routes-rest/package.json b/examples/api-routes-rest/package.json
index 581d565303a4f..2ccd1a3e68fec 100644
--- a/examples/api-routes-rest/package.json
+++ b/examples/api-routes-rest/package.json
@@ -7,10 +7,10 @@
"start": "next start"
},
"dependencies": {
- "isomorphic-unfetch": "3.0.0",
"next": "latest",
"react": "^16.8.6",
- "react-dom": "^16.8.6"
+ "react-dom": "^16.8.6",
+ "swr": "^0.1.18"
},
"license": "ISC"
}
diff --git a/examples/api-routes-rest/pages/api/users.js b/examples/api-routes-rest/pages/api/users.js
index a15d5b55403b6..288d6f422955e 100644
--- a/examples/api-routes-rest/pages/api/users.js
+++ b/examples/api-routes-rest/pages/api/users.js
@@ -1,11 +1,5 @@
// Fake users data
-const users = [
- {
- id: 1,
- },
- { id: 2 },
- { id: 3 },
-]
+const users = [{ id: 1 }, { id: 2 }, { id: 3 }]
export default (req, res) => {
// Get data from your database
diff --git a/examples/api-routes-rest/pages/index.js b/examples/api-routes-rest/pages/index.js
index c07b7eff60fdf..861919c5e3d84 100644
--- a/examples/api-routes-rest/pages/index.js
+++ b/examples/api-routes-rest/pages/index.js
@@ -1,23 +1,23 @@
-import fetch from 'isomorphic-unfetch'
+import useSwr from 'swr'
import Link from 'next/link'
-const Index = ({ users }) => (
-
-)
+const fetcher = url => fetch(url).then(res => res.json())
-Index.getInitialProps = async () => {
- const response = await fetch('http://localhost:3000/api/users')
- const users = await response.json()
+export default function Index() {
+ const { data, error } = useSwr('/api/users', fetcher)
- return { users }
-}
+ if (error) return Failed to load users
+ if (!data) return Loading...
-export default Index
+ return (
+
+ )
+}
diff --git a/examples/api-routes-rest/pages/user/[id].js b/examples/api-routes-rest/pages/user/[id].js
index 5a52a78e6ac2a..ab8bcfde799bf 100644
--- a/examples/api-routes-rest/pages/user/[id].js
+++ b/examples/api-routes-rest/pages/user/[id].js
@@ -1,12 +1,14 @@
-import fetch from 'isomorphic-unfetch'
+import { useRouter } from 'next/router'
+import useSwr from 'swr'
-const User = ({ user }) => {user.name}
+const fetcher = url => fetch(url).then(res => res.json())
-User.getInitialProps = async ({ query: { id } }, res) => {
- const response = await fetch(`http://localhost:3000/api/user/${id}`)
- const user = await response.json()
+export default function User() {
+ const router = useRouter()
+ const { data, error } = useSwr(`/api/user/${router.query.id}`, fetcher)
- return { user }
-}
+ if (error) return Failed to load user
+ if (!data) return Loading...
-export default User
+ return {data.name}
+}