Next.js: Functional Components vs ES6 Classes in NextJS

Created on 15 Jul 2018  路  1Comment  路  Source: vercel/next.js

All of the examples and tutorials of NextJs pages which presented in its docs and other references are with functional components.

So, What about classes? What happens to getInitialProps (or other features that mentioned in docs) when a functional component replaces with an ES6 class. Is that page still a NextJS page after replacing?

Most helpful comment

The contract is that you export a React component, which can be a function or a class, for example:

import React from 'react'
export default class SomePage extends React.Component {
  static async getInitialProps() {
    return {}
  }
  render() {
    return <div>test</div>
  }
}

Class components work fine when exported 馃憤

The docs mention exporting class components too btw: https://github.com/zeit/next.js#fetching-data-and-component-lifecycle

>All comments

The contract is that you export a React component, which can be a function or a class, for example:

import React from 'react'
export default class SomePage extends React.Component {
  static async getInitialProps() {
    return {}
  }
  render() {
    return <div>test</div>
  }
}

Class components work fine when exported 馃憤

The docs mention exporting class components too btw: https://github.com/zeit/next.js#fetching-data-and-component-lifecycle

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wagerfield picture wagerfield  路  3Comments

rauchg picture rauchg  路  3Comments

jesselee34 picture jesselee34  路  3Comments

formula349 picture formula349  路  3Comments

knipferrc picture knipferrc  路  3Comments