Skip to content

Commit 2a084f5

Browse files
authored
Warn about refs on lazy function components (#14645)
1 parent b5a3df6 commit 2a084f5

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎packages/react-reconciler/src/ReactFiberBeginWork.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,9 @@ function mountLazyComponent(
984984
letchild;
985985
switch(resolvedTag){
986986
caseFunctionComponent: {
987+
if(__DEV__){
988+
validateFunctionComponentInDev(workInProgress,Component);
989+
}
987990
child=updateFunctionComponent(
988991
null,
989992
workInProgress,

‎packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,4 +1064,28 @@ describe('ReactLazy', () => {
10641064
root.unstable_flushAll();
10651065
expect(root).toMatchRenderedOutput('2');
10661066
});
1067+
1068+
it('warns about ref on functions for lazy-loaded components',async()=>{
1069+
constLazyFoo=lazy(()=>{
1070+
constFoo=props=><div/>;
1071+
returnfakeImport(Foo);
1072+
});
1073+
1074+
constref=React.createRef();
1075+
constroot=ReactTestRenderer.create(
1076+
<Suspensefallback={<Texttext="Loading..."/>}>
1077+
<LazyFooref={ref}/>
1078+
</Suspense>,
1079+
{
1080+
unstable_isConcurrent: true,
1081+
},
1082+
);
1083+
1084+
expect(root).toFlushAndYield(['Loading...']);
1085+
expect(root).toMatchRenderedOutput(null);
1086+
awaitPromise.resolve();
1087+
expect(()=>{
1088+
expect(root).toFlushAndYield([]);
1089+
}).toWarnDev('Function components cannot be given refs');
1090+
});
10671091
});

0 commit comments

Comments
 (0)