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

Warn about refs on lazy function components #14645

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,9 @@ function mountLazyComponent(
let child;
switch (resolvedTag) {
case FunctionComponent: {
if (__DEV__) {
validateFunctionComponentInDev(workInProgress, Component);
}
child = updateFunctionComponent(
null,
workInProgress,
Expand Down
24 changes: 24 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,4 +1064,28 @@ describe('ReactLazy', () => {
root.unstable_flushAll();
expect(root).toMatchRenderedOutput('2');
});

it('warns about ref on functions for lazy-loaded components', async () => {
const LazyFoo = lazy(() => {
const Foo = props => <div />;
return fakeImport(Foo);
});

const ref = React.createRef();
const root = ReactTestRenderer.create(
<Suspense fallback={<Text text="Loading..." />}>
<LazyFoo ref={ref} />
</Suspense>,
{
unstable_isConcurrent: true,
},
);

expect(root).toFlushAndYield(['Loading...']);
expect(root).toMatchRenderedOutput(null);
await Promise.resolve();
expect(() => {
expect(root).toFlushAndYield([]);
}).toWarnDev('Function components cannot be given refs');
});
});