Skip to content

Commit

Permalink
Document getInitialProps error
Browse files Browse the repository at this point in the history
  • Loading branch information
HaNdTriX committed Aug 9, 2018
1 parent aaa694d commit 212c44f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions errors/get-inital-props-as-an-instance-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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)
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function loadGetInitialProps (Component, ctx) {
if (process.env.NODE_ENV !== 'production') {
if (Component.prototype && Component.prototype.getInitialProps) {
const compName = getDisplayName(Component)
const message = `"${compName}.getInitialProps()" is defined as an instance method.`
const message = `"${compName}.getInitialProps()" is defined as an instance method - visit https://err.sh/next.js/get-inital-props-as-an-instance-method for more information.`
throw new Error(message)
}
}
Expand Down

0 comments on commit 212c44f

Please sign in to comment.