next.js icon indicating copy to clipboard operation
next.js copied to clipboard

with-flow getInitialProps issue property `getInitialProps`. Property not found in 21: Index.getInitialProps = (context) => { statics of Index

Open kvarc7 opened this issue 9 years ago • 4 comments

I'm trying to integrate Flow with my current next project I notice there is a issue with getInitialProps method. Flow complains: screen shot 2017-11-25 at 13 49 09

I'm a little bit confused do I have to specify static getInitialProps: (ctx: {pathname: string, query: any, req?: any, res?: any, jsonPageRes?: any, err?: any}) => Promise<any>; for every page or it could be done with next.js.flow file? I see there is definition within the file

declare module "next/document" {
  declare export var Head: Class<React$Component<any, any>>;
  declare export var Main: Class<React$Component<any, any>>;
  declare export var NextScript: Class<React$Component<any, any>>;
  declare export default Class<React$Component<any, any>> & {
    getInitialProps: (ctx: {pathname: string, query: any, req?: any, res?: any, jsonPageRes?: any, err?: any}) => Promise<any>;
    renderPage(cb: Function): void;
  };
}

But it seems this definition already have some problems. Please note I tried with flow-bin version specified in the example, but I also tried with newest flow-bin and it seems the same.

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

I think this has to work automatically if it's well defined within the next.js.flow file

Steps to Reproduce (for bugs)

  1. Take with-flow example
  2. Change index.js page to be statefull component and add getInitialProps to it
  3. run yarn flow

Your Environment

Tech Version
next latest
node 8.7.0
OS OSX

kvarc7 avatar Nov 25 '17 13:11 kvarc7

@davscro have you found a solution to this? I'm setting up a new project and ran into something similar.

adrianmcli avatar Dec 15 '17 23:12 adrianmcli

@adrianmcli I just put static getInitialProps: (any) => any; in every page. IE:

class Index extends React.Component<Props, State> {

    static getInitialProps: (any) => any;

    render() {

But I think there must be a better way of doing it, some global pattern or something 🤔

kvarc7 avatar Dec 16 '17 09:12 kvarc7

Here is an example of how to type getInitialProps from the docs. https://github.com/zeit/next.js/blob/canary/examples/with-flow/flow-typed/next.js.flow

SimplyComplexable avatar Dec 13 '18 17:12 SimplyComplexable

I know this issue is closed but this is what my logic dictates me

type Props = { title: string };
type initialProps: (ctx: {[string]: any}) => Promise<Props>;

class Index extends React.Component<Props>{
  static getInitialProps: initialProps;

  ...
}

Index.getInitialProps = async (ctx) => {
   ...
   return { title: "Hi" };
}

EzeRangel avatar Mar 04 '19 19:03 EzeRangel