File tree 2 files changed +55
-0
lines changed
2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { GetServerSideProps } from 'next' ;
2
+ import React from 'react' ;
3
+
4
+ const ApiExample : React . FC < { data : any ; error : string | null } > = ( { data, error } ) => {
5
+ if ( error ) {
6
+ return < div > Error: { error } </ div > ;
7
+ }
8
+
9
+ return (
10
+ < div >
11
+ < h1 > API Example</ h1 >
12
+ < div >
13
+ < h2 > Title: { data ?. title } </ h2 >
14
+ < p > Body: { data ?. body } </ p >
15
+ </ div >
16
+ </ div >
17
+ ) ;
18
+ } ;
19
+
20
+ export const getServerSideProps : GetServerSideProps = async ( ) => {
21
+ try {
22
+ const response = await fetch ( 'https://jsonplaceholder.typicode.com/posts/1' ) ;
23
+ if ( ! response . ok ) {
24
+ throw new Error ( 'Failed to fetch data' ) ;
25
+ }
26
+ const jsonData = await response . json ( ) ;
27
+ return {
28
+ props : {
29
+ data : jsonData ,
30
+ error : null ,
31
+ } ,
32
+ } ;
33
+ } catch ( error ) {
34
+ return {
35
+ props : {
36
+ data : null ,
37
+ error : error . message ,
38
+ } ,
39
+ } ;
40
+ }
41
+ } ;
42
+
43
+ export default ApiExample ;
Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+
3
+ function Example ( ) {
4
+ return (
5
+ < div >
6
+ < h1 > This is an Example Page</ h1 >
7
+ < p > Welcome to my simple Next.js page!</ p >
8
+ </ div >
9
+ ) ;
10
+ }
11
+
12
+ export default Example ;
You can’t perform that action at this time.
0 commit comments