Skip to content

Commit 7699cbe

Browse files
impronunciablearunoda
authored andcommitted
Add/readme example stateless lifecycle (vercel#1558)
* Using developit/unfetch as the Fetch API polyfill * Added a stateless component with getInitialProps in the readme. * Fixed typo
1 parent 1aabe4b commit 7699cbe

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,20 @@ For the initial page load, `getInitialProps` will execute on the server only. `g
215215

216216
_Note: `getInitialProps` can **not** be used in children components. Only in `pages`._
217217

218+
You can also define the `getInitialProps` lifecycle method for stateless components:
219+
220+
```jsx
221+
const Page = ({ stars }) => <div>Next stars: {stars}</div>
222+
223+
Page.getInitialProps = async ({ req }) => {
224+
const res = await fetch('https://api.github.com/repos/zeit/next.js')
225+
const json = await res.json()
226+
return { stars: json.stargazers_count }
227+
}
228+
229+
export default Page
230+
```
231+
218232
`getInitialProps` receives a context object with the following properties:
219233

220234
- `pathname` - path section of URL

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy