question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`next/app` as functional component

See original GitHub issue

Feature request

Problem

Functional components yield a more performant and readable code. It’s been hinted by React team that class’es may be split into a separate package in the near future. It would be nice for Next.js to provide a way to create apps without classes. Currently it’s not possible as _app.js and _document.js require to extend Next’s React classes.

Solution

If Next’s controlling components can be rewritten with React Hooks, the entire Next app should consist of functional components by default. Perhaps we could have a version that uses that style as an opt-in for backwards compatibility.

Alternative

I am considering rewriting the above-mentioned components in my project folder, but I fear that it may cause some issues.

Additional context

This is pretty much it.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:126
  • Comments:28 (11 by maintainers)

github_iconTop GitHub Comments

31reactions
myeongjae-kimcommented, Apr 17, 2020

Here is the perfect _app type.

// import App from "next/app";
import { NextComponentType } from "next"
import { AppContext, AppInitialProps, AppProps } from "next/app";

const MyApp: NextComponentType<AppContext, AppInitialProps, AppProps> = ({ Component, pageProps }) => {
  return <Component {...pageProps} />
}

// Only uncomment this method if you have blocking data requirements for
// every single page in your application. This disables the ability to
// perform automatic static optimization, causing every page in your app to
// be server-side rendered.

// MyApp.getInitialProps = async (appContext) => {
//   const appProps = await App.getInitialProps(appContext)
//   return { ...appProps }
// }

export default MyApp;

https://github.com/myeongjae-kim/next-js-with-typescript-valid-app-type

21reactions
timneutkenscommented, Sep 23, 2019

@medmin please don’t spam issues.

You can already use hooks in _app.js by doing:

import React from 'react'
import App from 'next/app'

function MyComponent({children}) {
  // You can use hooks here
  return <>{children}</> // The fragment is just illustrational
}

class MyApp extends App {
  // Only uncomment this method if you have blocking data requirements for
  // every single page in your application. This disables the ability to
  // perform automatic static optimization, causing every page in your app to
  // be server-side rendered.
  //
  // static async getInitialProps(appContext) {
  //   // calls page's `getInitialProps` and fills `appProps.pageProps`
  //   const appProps = await App.getInitialProps(appContext);
  //
  //   return { ...appProps }
  // }

  render() {
    const { Component, pageProps } = this.props
    return <MyComponent>
      <Component {...pageProps} />
    </MyComponent>
  }
}

export default MyApp

We’ve already made changes to Next.js to allow the exported component itself to be a functional component. However this would break withRouter as that still uses legacy context for backwards compat reasons.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Custom `App` - Next.js
The Component prop is the active page , so whenever you navigate between routes, Component will change to the new page . Therefore,...
Read more >
Next.Js Series #4 - What is the custom 'App' component in ...
We are going to introduce the custom 'App' component in Next.Js and its use cases. When we used 'create-next-app' command to create our...
Read more >
How to manage function component state in NextJS?
I wanna to access a state variable of component ...
Read more >
Next.js — Start web development with Next.js & create-next-app
After you create your application with create-next-app it is ready to use in seconds. ... Functional vs Class Components in React 16.9.
Read more >
Best practices to increase the speed for Next.js apps
This function will be called by the server on every request, returning an object that will be passed to the page component as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found