Skip to content
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

React.lazy has an incorrect type definition #7219

Closed
nicholas-l opened this issue Nov 28, 2018 · 6 comments
Closed

React.lazy has an incorrect type definition #7219

nicholas-l opened this issue Nov 28, 2018 · 6 comments
Labels

Comments

@nicholas-l
Copy link

nicholas-l commented Nov 28, 2018

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>;
@jbrown215 jbrown215 added the bug label Nov 28, 2018
@jbrown215
Copy link
Contributor

@bvaughn: Mind helping out here?

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

@jbrown215
Copy link
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?

@bvaughn
Copy link
Contributor

bvaughn commented Nov 28, 2018

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

@nicholas-l
Copy link
Author

@bvaughn yes, go for it :)

@alexeyraspopov
Copy link

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

@jbrown215
Copy link
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.

@jbrown215 jbrown215 changed the title Flow error when returning Promise in React.lazy React.lazy has an incorrect type definition Nov 28, 2018
gabelevi pushed a commit that referenced this issue Dec 4, 2018
Summary:
Resolves #7219

- [x] 2fdcda5: Removed `React.useMutationEffect` hook (facebook/react/pull/14336) and updated tests. This hook was only ever exposed in an alpha release of 16.7 and so it should be safe to remove in a non-backwards-compatible way.
- [x] dabf49e: Add `React.Suspense` component and tests. (No final public documentation exists for `Suspense` yet– although we're working on it. For the time being, [tests like this](https:/facebook/react/blob/master/packages/react-reconciler/src/__tests__/ReactSuspense-test.internal.js) can serve as a reference.)
- [x] 8df05bd: Fixed invalid `useCallback` definition and tests.
- [x] 063b022: Fixed invalid `React.lazy` signature to reflect that a `Promise` with a "default" property should be returned (rather than a React component).
Pull Request resolved: #7213

Reviewed By: jbrown215

Differential Revision: D13222880

Pulled By: bvaughn

fbshipit-source-id: 9e1b48ace984051928c4ab92fa61182ad70f7119
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants