Skip to content

React.lazy has an incorrect type definition #7219

Closed

Description

@nicholas-l

I am getting an error when using React.lazy:

Cannot call `React.lazy` with function bound to `component` because in the return value:
 - Either a callable signature is missing in `Promise` [1] but exists in `React.StatelessFunctionalComponent` [2].
 - Or `Promise` [1] is incompatible with statics of `React.Component` [3].

as an example:

const asyncGraphiQL = React.lazy(() =>
  import(/* webpackChunkName: "graphiql" */ './containers/graphiql')
);

Should line be returning a promise instead?

declare export function lazy<P>(
    component: () => Promise<{ default: React$ComponentType<P> }>,
  ): React$ComponentType<P>;

Activity

  1. jbrown215 commented on Nov 28, 2018

    @jbrown215
    Contributor

    @bvaughn: Mind helping out here?

    Is it always meant to render a {default: React$ComponentType<P>} from an export?

  2. jbrown215 commented on Nov 28, 2018

    @jbrown215
    Contributor

    Ah, from the react docs:

    React.lazy takes a function that must call a dynamic import(). This must return a Promise which resolves to a module with a default export containing a React component.

    The type here is wrong. Your type is right. Want to put up a PR?

  3. bvaughn commented on Nov 28, 2018

    @bvaughn

    I'd be happy to add this change to my PR if it's easier for everyone.

  4. nicholas-l commented on Nov 28, 2018

    @nicholas-l
    Author

    @bvaughn yes, go for it :)

  5. alexeyraspopov commented on Nov 28, 2018

    @alexeyraspopov

    Also, "StatelessFunctionalComponent" seems to be using the old terminology. I guess we can change it to "FunctionComponent", right?

  6. jbrown215 commented on Nov 28, 2018

    @jbrown215
    Contributor

    @alexeyraspopov: Yes, but that's a very disruptive and breaking change. We certainly should change the name, but we should have a codemod ready to go along with the release.

  7. changed the title [-]Flow error when returning Promise in React.lazy[/-] [+]React.lazy has an incorrect type definition[/+] on Nov 28, 2018
  8. added a commit that references this issue on Dec 4, 2018
    5e6beef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions