Next.js: Warn on using getInitialProps in child components

Created on 22 Feb 2019  路  3Comments  路  Source: vercel/next.js

Feature request

Is your feature request related to a problem? Please describe.

In documentation we see that the getInitialProps method shouldn't be used outside of /pages directory. This was a little surprise for me as I didn't notice this note in the first read of docs.

Describe the solution you'd like

If getInitialProps is used in child component of /pages component it should throw error like you're using getInitialProps in child component, which can introduce issues please see documentation for more information

Describe alternatives you've considered

adding seperate getInitalProps section to docs to highlight the gotchas

needs investigation

Most helpful comment

It's actually hard to do this because it's valid for the user to implement a higher order component that implements getInitialProps. I do think it would be a huge help for new users to have a warning for this, so maybe we need to have a way to opt-out of the warning 馃

All 3 comments

It's actually hard to do this because it's valid for the user to implement a higher order component that implements getInitialProps. I do think it would be a huge help for new users to have a warning for this, so maybe we need to have a way to opt-out of the warning 馃

Thanks for pointing it out! The issue I have is I feel it's not obvious enough that this shouldn't be used in child components. Is there currently a way to test if getInitialProps is being called in child component? If not maybe adding more info to documentation on this method would be enough

Going to close this in favor of a RFC @timer is drafting.

Was this page helpful?
0 / 5 - 0 ratings