mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
20dc57391e
## Summary Update code block language in [error pages](https://github.com/vercel/next.js/tree/canary/errors). ### Improving Documentation - [x] Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - [x] Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
42 lines
729 B
Plaintext
42 lines
729 B
Plaintext
---
|
|
title: '`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.
|
|
|
|
```jsx filename="pages/example.js"
|
|
export default class YourEntryComponent extends React.Component {
|
|
static getInitialProps() {
|
|
return {}
|
|
}
|
|
|
|
render() {
|
|
return 'foo'
|
|
}
|
|
}
|
|
```
|
|
|
|
or
|
|
|
|
```jsx filename="pages/example.js"
|
|
const YourEntryComponent = function () {
|
|
return 'foo'
|
|
}
|
|
|
|
YourEntryComponent.getInitialProps = () => {
|
|
return {}
|
|
}
|
|
|
|
export default YourEntryComponent
|
|
```
|
|
|
|
## Useful Links
|
|
|
|
- [Fetching data and component lifecycle](/docs/pages/api-reference/functions/get-initial-props)
|