mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
d1b67623f6
* Throw error if getInitialProps is defined as as instance method Omitting the static keyword happens pretty often. Therefore we should trigger a warning in devmode. Closes: #4782 * Document getInitialProps error * Add unit tests for loadGetInitialProps
40 lines
678 B
Markdown
40 lines
678 B
Markdown
# getInitialProps was defined as an instance method
|
||
|
||
#### Why This Error Occurred
|
||
|
||
`getInitialProps` must be a static method in order to be called by next.js.
|
||
|
||
#### Possible Ways to Fix It
|
||
|
||
Use the static keyword.
|
||
|
||
```js
|
||
export default class YourEntryComponent extends React.Component {
|
||
static getInitialProps () {
|
||
return {}
|
||
}
|
||
|
||
render () {
|
||
return 'foo'
|
||
}
|
||
}
|
||
```
|
||
|
||
or
|
||
|
||
```js
|
||
const YourEntryComponent = function () {
|
||
return 'foo'
|
||
}
|
||
|
||
YourEntryComponent.getInitialProps = () => {
|
||
return {}
|
||
}
|
||
|
||
export default YourEntryComponent
|
||
```
|
||
|
||
### Useful Links
|
||
|
||
- [Fetching data and component lifecycle](https://github.com/zeit/next.js#fetching-data-and-component-lifecycle)
|