-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next/app
as functional component
#7515
Comments
This would be great! Would love to be able to use hooks in |
This comment has been minimized.
This comment has been minimized.
@medmin please don't spam issues. You can already use hooks in 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 |
@timneutkens do you mean It'll break |
Whenever it's being used. Mostly related to Apollo and it's tree traversing (if you're using Apollo). Hence why we can't get rid of the extending of the original |
@timneutkens Can you expand on the issue? Or point to somewhere it's discussed? Also, what's the plan to move forward? |
@mikestopcontinues it's been solved since my last comment and the latest release of Next.js (9.1.2). I've opened a PR to update the docs. |
Very cool. Thanks! |
Side note, it appears dynamic routing does not provide a query to getInitialProps when using this functional app. If this is not a known bug I can open a new issue with more details. |
This issue is closed and marked resolved but there were only changes to Is it this? import Document, { Html, Head, Main, NextScript } from 'next/document'
import { AppInitialProps } from 'next/app'
const AppDocument = ({ ...initialProps }: Document & AppInitialProps) => {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
export default AppDocument |
Document should be a class extending from next/document as documented. This issue is completely unrelated to _document. |
I've seen the documentation has been updated in this PR that was merged in October 31, but I can't see it yet in the official documentation By the way, which should be the types for I'm currently using this:
Which seems to work fine, but I'm not sure if this is the best approach, particularly regarding |
This comment has been minimized.
This comment has been minimized.
@theBashShell as said, extend from next/document, don't do what you posted as I can guarantee it will break 100% in any future update. Hid the comment for that reason. |
you can easily type it like this:
|
I don't think this is right. I'm leaving it untyped at the moment until I find the right type. For reference, this is
and the
|
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 |
Seems this is just for typescript only. I am using javascript |
TypeScript is a super set of JavaScript, so just delete types then you can use it for JavaScript. // import App from 'next/app'
function MyApp({ 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) => {
// // calls page's `getInitialProps` and fills `appProps.pageProps`
// const appProps = await App.getInitialProps(appContext);
//
// return { ...appProps }
// }
export default MyApp Also, be careful to use getInitialProps(I think you may know, but as a caution). getInitialProps of |
The documentation covers _app as a function already: https://nextjs.org/docs/advanced-features/custom-app Commented out getInitialProps from your examples @myeongjae-kim as in general adding it is a bad default if there's nothing happening in it. |
This comment has been minimized.
This comment has been minimized.
Don't want to open a separate issue just yet but the OP was also talking about
Do we know what's necessary to solve this for |
I'm also interested to have the custom Document as a functional component... Is this possible like the custom app? |
@NMinhNguyen I think it's probably worth opening a separate issue for this, as firstly, this issue is closed, and secondly, the title is about |
me2 |
Hid the last comment as it will likely cause your application to crash between upgrades in backwards incompatible ways because it does not follow the documented way of using _document. Converting
Created #19355 to track this. |
Is the solution added in this recent PR? #28515 |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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.
The text was updated successfully, but these errors were encountered: