Skip to content

Commit 1084164

Browse files
committed
init
1 parent e533a6e commit 1084164

File tree

7 files changed

+66
-35
lines changed

7 files changed

+66
-35
lines changed

app/client/page.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ export default function Client(props) {
1616
</>
1717
)
1818
}
19-
return <h1>client Page</h1>
19+
20+
return (
21+
<>
22+
<h1>client Page</h1>
23+
</>
24+
)
2025
}
2126

2227
// getServerSideProps is not supported

app/components/client.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use client'
2+
3+
export default function ClientComponent(props) {
4+
console.log('🚀 ~ ClientComponent')
5+
6+
if (props.children) {
7+
return (
8+
<>
9+
<h1>client component</h1>
10+
{props.children}
11+
</>
12+
)
13+
}
14+
15+
return <h1>client component</h1>
16+
}

app/components/server.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Home from './home'
2+
3+
export default function ServerComponent({ data }) {
4+
console.log('🚀 ~ ServerComponent')
5+
6+
return (
7+
<>
8+
<h1>Server component</h1>
9+
<Home data={data} />
10+
</>
11+
)
12+
}

app/mix/page.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import Client from '../client/page'
2-
import Server from '../server/page'
1+
import { getData } from '../../lib/fetch-data'
2+
import ClientComponent from '../components/client'
3+
import ServerComponent from '../components/server'
34

4-
export default function MixMatchPage() {
5+
export default async function MixMatchPage() {
56
console.log('🚀 ~ file: page.js:5 ~ MixMatchPage')
7+
const data = await getData()
8+
69
return (
710
<div>
811
<h1>Mix Pages</h1>
912
<div className="box-blue">
10-
<Client>
11-
<Server />
12-
</Client>
13+
<ClientComponent>
14+
<ServerComponent data={data} />
15+
</ClientComponent>
1316
</div>
1417
</div>
1518
)

app/server/page.js

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
1-
import Home from "../components/home"
2-
3-
async function getData() {
4-
const res = await fetch(
5-
`https://5fbc07c3c09c200016d41656.mockapi.io/api/v1/games`
6-
)
7-
8-
if (!res.ok) {
9-
throw new Error('Failed to fetch data')
10-
}
11-
12-
console.log('🚀 fetch on server data')
13-
return res.json()
14-
}
1+
import ServerComponent from '../components/server'
2+
import { getData } from '../../lib/fetch-data'
153

164
export default async function Server() {
175
console.log('============== Server page ======================')
186
const data = await getData()
197

20-
return (
21-
<>
22-
<h1>Server Page</h1>
23-
<Home data={data} />
24-
</>
25-
)
8+
return <ServerComponent data={data} />
269
}

lib/fetch-data.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
export default async function fetchData(type) {
2-
const res = await fetch(`https://hacker-news.firebaseio.com/v0/${type}.json`);
2+
const res = await fetch(`https://hacker-news.firebaseio.com/v0/${type}.json`)
33

44
if (res.status !== 200) {
5-
throw new Error(`Status ${res.status}`);
5+
throw new Error(`Status ${res.status}`)
66
}
7-
return res.json();
7+
return res.json()
8+
}
9+
10+
export const getData = async () => {
11+
const res = await fetch(
12+
`https://5fbc07c3c09c200016d41656.mockapi.io/api/v1/games`
13+
)
14+
15+
if (!res.ok) {
16+
throw new Error('Failed to fetch data')
17+
}
18+
19+
return res.json()
820
}

pages/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import Client from '../app/client/page'
2-
import Home from '../app/components/home'
1+
import ClientComponent from '../app/components/client'
2+
import ServerComponent from '../app/components/server'
33

44
export default function Page({ data }) {
55
console.log('🚀 pages => index.js')
66

77
return (
88
<>
99
<h1>Client component (getServerSideProps)</h1>
10-
<Client>
11-
<Home data={data} />
12-
</Client>
10+
<ClientComponent>
11+
<ServerComponent data={data} />
12+
</ClientComponent>
1313
</>
1414
)
1515
}

0 commit comments

Comments
 (0)