From 3ef748abb3a540c93b7aeadc402e80f7478e4e34 Mon Sep 17 00:00:00 2001 From: Brandon Dail Date: Tue, 6 Sep 2016 03:49:20 -0500 Subject: [PATCH] Use compositeType in warning invariant for refs (#7658) (cherry picked from commit 06ea71d3fdd614e12c659c2f08f371c6b01fe880) --- .../reconciler/ReactCompositeComponent.js | 4 ++- .../__tests__/ReactTestRenderer-test.js | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js b/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js index 9536502084..6ee4e2aac7 100644 --- a/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js +++ b/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js @@ -1097,7 +1097,9 @@ var ReactCompositeComponentMixin = { if (__DEV__) { var componentName = component && component.getName ? component.getName() : 'a component'; - warning(publicComponentInstance != null, + warning( + publicComponentInstance != null || + component._compositeType !== CompositeTypes.StatelessFunctional, 'Stateless function components cannot be given refs ' + '(See ref "%s" in %s created by %s). ' + 'Attempts to access this ref will fail.', diff --git a/src/renderers/testing/__tests__/ReactTestRenderer-test.js b/src/renderers/testing/__tests__/ReactTestRenderer-test.js index e562867fd2..95f7f9f4d9 100644 --- a/src/renderers/testing/__tests__/ReactTestRenderer-test.js +++ b/src/renderers/testing/__tests__/ReactTestRenderer-test.js @@ -206,6 +206,31 @@ describe('ReactTestRenderer', function() { expect(log).toEqual([null]); }); + it('warns correctly for refs on SFCs', function() { + spyOn(console, 'error'); + function Bar() { + return
Hello, world
+ } + class Foo extends React.Component { + render() { + return + } + } + class Baz extends React.Component { + render() { + return
+ } + } + ReactTestRenderer.create(); + ReactTestRenderer.create(); + expect(console.error.calls.count()).toBe(1); + expect(console.error.calls.argsFor(0)[0]).toContain( + 'Stateless function components cannot be given refs ' + + '(See ref "foo" in Bar created by Foo). ' + + 'Attempts to access this ref will fail.' + ); + }); + it('supports error boundaries', function() { var log = []; class Angry extends React.Component {