File tree 7 files changed +66
-35
lines changed
7 files changed +66
-35
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,12 @@ export default function Client(props) {
16
16
</ >
17
17
)
18
18
}
19
- return < h1 > client Page</ h1 >
19
+
20
+ return (
21
+ < >
22
+ < h1 > client Page</ h1 >
23
+ </ >
24
+ )
20
25
}
21
26
22
27
// getServerSideProps is not supported
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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'
3
4
4
- export default function MixMatchPage ( ) {
5
+ export default async function MixMatchPage ( ) {
5
6
console . log ( '🚀 ~ file: page.js:5 ~ MixMatchPage' )
7
+ const data = await getData ( )
8
+
6
9
return (
7
10
< div >
8
11
< h1 > Mix Pages</ h1 >
9
12
< div className = "box-blue" >
10
- < Client >
11
- < Server />
12
- </ Client >
13
+ < ClientComponent >
14
+ < ServerComponent data = { data } />
15
+ </ ClientComponent >
13
16
</ div >
14
17
</ div >
15
18
)
Original file line number Diff line number Diff line change 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'
15
3
16
4
export default async function Server ( ) {
17
5
console . log ( '============== Server page ======================' )
18
6
const data = await getData ( )
19
7
20
- return (
21
- < >
22
- < h1 > Server Page</ h1 >
23
- < Home data = { data } />
24
- </ >
25
- )
8
+ return < ServerComponent data = { data } />
26
9
}
Original file line number Diff line number Diff line change 1
1
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` )
3
3
4
4
if ( res . status !== 200 ) {
5
- throw new Error ( `Status ${ res . status } ` ) ;
5
+ throw new Error ( `Status ${ res . status } ` )
6
6
}
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 ( )
8
20
}
Original file line number Diff line number Diff line change 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 '
3
3
4
4
export default function Page ( { data } ) {
5
5
console . log ( '🚀 pages => index.js' )
6
6
7
7
return (
8
8
< >
9
9
< h1 > Client component (getServerSideProps)</ h1 >
10
- < Client >
11
- < Home data = { data } />
12
- </ Client >
10
+ < ClientComponent >
11
+ < ServerComponent data = { data } />
12
+ </ ClientComponent >
13
13
</ >
14
14
)
15
15
}
You can’t perform that action at this time.
0 commit comments