|
| 1 | +--- |
| 2 | +slug: react-server-component |
| 3 | +title: What is React Server Component |
| 4 | +author: Bunlong |
| 5 | +author_title: React Patterns Team |
| 6 | +author_url: https://github.com/Bunlong |
| 7 | +author_image_url: https://avatars1.githubusercontent.com/u/37023003?s=400&u=0049c6773040efb265cdf622076305f8b47facec&v=4 |
| 8 | +tags: [server] |
| 9 | +image: /img/react-server-component.png |
| 10 | +--- |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +## What is React Server Component? |
| 15 | + |
| 16 | +React Server Component (CSR) is a new component in React. |
| 17 | + |
| 18 | +Recently, All components in React are client-side component (MyComponent.js) but now you can create them as server-side component by just adding `.server` (MyComponent.server.js). |
| 19 | + |
| 20 | +Anyway client-size component and server-size component work together very well. |
| 21 | + |
| 22 | +<!--truncate--> |
| 23 | + |
| 24 | +## What new in React Server Component? |
| 25 | + |
| 26 | +### React Server Component allows static content render faster. |
| 27 | + |
| 28 | +Server-side components can't have any interactivity like using `useState` hook but they can import client-size components with which the user interact. |
| 29 | + |
| 30 | +### Won't affect the bundle size |
| 31 | + |
| 32 | +Whatever library you include inside your server component as well as the text content it won't affect the bundle size because it is render on the server. |
| 33 | + |
| 34 | +### Accessing database directly |
| 35 | + |
| 36 | +Imagine accesing database directly from your components instead of calling API. |
| 37 | + |
| 38 | +Accessing data by calling API: |
| 39 | + |
| 40 | +```js |
| 41 | +const users = fetch('http://localhost:8000/users).json(); |
| 42 | +``` |
| 43 | +
|
| 44 | +You can access your data as below: |
| 45 | +
|
| 46 | +```js |
| 47 | +const users = db.query( |
| 48 | + `SELECT * FROM users WHERE name LIKE $1`, |
| 49 | + ['%' + searchText + '%'] |
| 50 | +).rows; |
| 51 | +``` |
| 52 | +
|
| 53 | +The feature is still in the experimental phase so keep an eye on the upcoming version of React. |
0 commit comments