Skip to content

Commit

Permalink
Warn about refs on lazy function components (#14645)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Jan 21, 2019
1 parent b5a3df6 commit 2a084f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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');
});
});

0 comments on commit 2a084f5

Please sign in to comment.