Skip to content

Commit f0367d4

Browse files
committed
add test page and call api
1 parent 8924148 commit f0367d4

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

pages/api-example.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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;

pages/example.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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;

0 commit comments

Comments
 (0)