Commit 7699cbe9 authored by Dan Zajdband's avatar Dan Zajdband Committed by Arunoda Susiripala

Add/readme example stateless lifecycle (#1558)

* Using developit/unfetch as the Fetch API polyfill

* Added a stateless component with getInitialProps in the readme.

* Fixed typo
parent 1aabe4bc
...@@ -215,6 +215,20 @@ For the initial page load, `getInitialProps` will execute on the server only. `g ...@@ -215,6 +215,20 @@ For the initial page load, `getInitialProps` will execute on the server only. `g
_Note: `getInitialProps` can **not** be used in children components. Only in `pages`._ _Note: `getInitialProps` can **not** be used in children components. Only in `pages`._
You can also define the `getInitialProps` lifecycle method for stateless components:
```jsx
const Page = ({ stars }) => <div>Next stars: {stars}</div>
Page.getInitialProps = async ({ req }) => {
const res = await fetch('https://api.github.com/repos/zeit/next.js')
const json = await res.json()
return { stars: json.stargazers_count }
}
export default Page
```
`getInitialProps` receives a context object with the following properties: `getInitialProps` receives a context object with the following properties:
- `pathname` - path section of URL - `pathname` - path section of URL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment