|
| 1 | +# Notes |
| 2 | + |
| 3 | +Common examples of when you may want to use this type of pre-rendering are: displaying a list of articles on a blog homepage, displaying a list of products on a homepage, or displaying a list of topics on a documentation page. |
| 4 | + |
| 5 | +All you have to do is define the async function _getStaticProps_ fetch your data within the function and return an object with the necessary props. The props will then be availabe for use in your component |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +### First Steps |
| 12 | + |
| 13 | +- delete API folder from Pages |
| 14 | +- delete home.module.css from styles |
| 15 | +- changed index.js file content into simple component that returns a plain h1 tag. |
| 16 | + |
| 17 | +Using the users data from [JSONPlaceholder](https://jsonplaceholder.typicode.com/) for test APIs |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +- create a new file in Pages folder named "users.js" |
| 22 | + |
| 23 | + - within the file, defined and exported UserList compontent |
| 24 | + |
| 25 | +- in Next, when you export a page component, you can also export an async function called "getStaticProps" |
| 26 | + |
| 27 | + - This function will run at build time in production. Inside the function, you can fetch external data and send it as "props" to the page. |
| 28 | + |
| 29 | +- go to users.js and export the getStaticProps async function |
| 30 | +- pass in the JSONPaceholder/users URL as an argument. |
| 31 | +- once you have the reponse, you convert it into JSON and log it to the console |
| 32 | +- then, you need to pass the data to the component above |
| 33 | +- this is accomplished via a return object |
| 34 | +- the object contains another object named 'props' that can contain any key value pairs, which will be automatically injected as props into the component. |
| 35 | + - in our case, we set one property named 'users' which is set to 'data' |
| 36 | + - when we return this, the userlist component will receive the props at build time |
| 37 | + - so, you pass in 'props' as an arugment. you could also destructure the users property. |
| 38 | +- To render |
| 39 | + - wrap the UserList return in a React fragment |
| 40 | + - map over the list of user |
| 41 | + - for each user, we're returning a div tag where the key tag is set to user.id |
| 42 | + - then, we're rendering user.name and user.email |
| 43 | + - you could map more, but we're only using name and email in this example |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Original README info from create-next-app |
| 48 | + |
1 | 49 | This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). |
2 | 50 |
|
3 | 51 | ## Getting Started |
|
0 commit comments