2023-07-05 15:11:16 +02:00
|
|
|
---
|
|
|
|
title: '`getInitialProps` was defined as an instance method'
|
|
|
|
---
|
2018-08-09 19:13:44 +02:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Why This Error Occurred
|
2018-08-09 19:13:44 +02:00
|
|
|
|
|
|
|
`getInitialProps` must be a static method in order to be called by next.js.
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Possible Ways to Fix It
|
2018-08-09 19:13:44 +02:00
|
|
|
|
|
|
|
Use the static keyword.
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```js filename="pages/example.js"
|
2018-08-09 19:13:44 +02:00
|
|
|
export default class YourEntryComponent extends React.Component {
|
2019-05-29 13:57:26 +02:00
|
|
|
static getInitialProps() {
|
2018-08-09 19:13:44 +02:00
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
render() {
|
2018-08-09 19:13:44 +02:00
|
|
|
return 'foo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
or
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
```js filename="pages/example.js"
|
2020-05-18 21:24:37 +02:00
|
|
|
const YourEntryComponent = function () {
|
2018-08-09 19:13:44 +02:00
|
|
|
return 'foo'
|
|
|
|
}
|
|
|
|
|
|
|
|
YourEntryComponent.getInitialProps = () => {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default YourEntryComponent
|
|
|
|
```
|
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
## Useful Links
|
2018-08-09 19:13:44 +02:00
|
|
|
|
2023-07-05 15:11:16 +02:00
|
|
|
- [Fetching data and component lifecycle](/docs/pages/api-reference/functions/get-initial-props)
|