Bläddra i källkod

feat: warn ref usage on functional components

Evan You 6 år sedan
förälder
incheckning
7f054782ad
1 ändrade filer med 8 tillägg och 0 borttagningar
  1. 8 0
      packages/runtime-core/src/renderer.ts

+ 8 - 0
packages/runtime-core/src/renderer.ts

@@ -804,6 +804,14 @@ export function createRenderer<
       }
       }
     }
     }
     if (n2.ref !== null && parentComponent !== null) {
     if (n2.ref !== null && parentComponent !== null) {
+      if (__DEV__ && !(n2.shapeFlag & ShapeFlags.STATEFUL_COMPONENT)) {
+        pushWarningContext(n2)
+        warn(
+          `Functional components do not support "ref" because they do not ` +
+            `have instances.`
+        )
+        popWarningContext()
+      }
       setRef(n2.ref, n1 && n1.ref, parentComponent, n2.component!.renderProxy)
       setRef(n2.ref, n1 && n1.ref, parentComponent, n2.component!.renderProxy)
     }
     }
   }
   }