Skip to content

Commit 3b99408

Browse files
2 parents 9fdeb7f + deb2320 commit 3b99408

File tree

219 files changed

+2644
-137879
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+2644
-137879
lines changed
Binary file not shown.
25.7 KB
Binary file not shown.
-776 Bytes
Binary file not shown.
-1.22 MB
Binary file not shown.
10 Bytes
Binary file not shown.
10 Bytes
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default function ClientDemo({ children }) {
2+
console.log('ClientDemo rendered');
3+
return (
4+
<div className='client-cmp'>
5+
<h2>A React Client Component</h2>
6+
<p>
7+
Will be rendered on the client <strong>AND</strong> the server.
8+
</p>
9+
{children}
10+
</div>
11+
);
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from 'node:fs/promises';
2+
3+
export default async function DataFetchingDemo() {
4+
const data = await fs.readFile('dummy-db.json', 'utf-8');
5+
const users = JSON.parse(data);
6+
7+
return (
8+
<div className='rsc'>
9+
<h2>RSC with Data Fetching</h2>
10+
<p>
11+
Uses <strong>async / await</strong> for data fetching.
12+
</p>
13+
<ul>
14+
{users.map((user) => (
15+
<li key={user.id}>
16+
{user.name} ({user.title})
17+
</li>
18+
))}
19+
</ul>
20+
</div>
21+
);
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default async function RSCDemo() {
2+
console.log('RSCDemo rendered');
3+
return (
4+
<div className='rsc'>
5+
<h2>A React Server Component</h2>
6+
<p>
7+
Will <strong>ONLY</strong> be rendered on the server or at build time.
8+
</p>
9+
<p>
10+
<strong>NEVER</strong> on the client-side!
11+
</p>
12+
</div>
13+
);
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'node:fs';
2+
3+
export default function ServerActionsDemo() {
4+
async function saveUserAction(formData) {
5+
'use server';
6+
const data = fs.readFileSync('dummy-db.json', 'utf-8');
7+
const instructors = JSON.parse(data);
8+
const newInstructor = {
9+
id: new Date().getTime().toString(),
10+
name: formData.get('name'),
11+
title: formData.get('title'),
12+
};
13+
14+
instructors.push(newInstructor);
15+
fs.writeFileSync('dummy-db.json', JSON.stringify(instructors));
16+
}
17+
18+
return (
19+
<div className='rsc'>
20+
<h2>Server Actions</h2>
21+
<p>
22+
A "Form Action" converted to a "Server Action" via{' '}
23+
<strong>"use server"</strong>.
24+
</p>
25+
<p>Can be defined in a server component or a separate file.</p>
26+
<p>Can be called from inside server component or client component.</p>
27+
<form action={saveUserAction}>
28+
<p>
29+
<label htmlFor='name'>User name</label>
30+
<input type='text' id='name' name='name' required />
31+
</p>
32+
<p>
33+
<label htmlFor='title'>Title</label>
34+
<input type='text' id='title' name='title' required />
35+
</p>
36+
<p>
37+
<button>Save User</button>
38+
</p>
39+
</form>
40+
</div>
41+
);
42+
}

0 commit comments

Comments
 (0)