From 63cfcca74eb9805720968e69b18c09f4c80e188d Mon Sep 17 00:00:00 2001 From: Bradley Spaulding Date: Fri, 25 Sep 2015 16:21:00 -0700 Subject: [PATCH] Updated stateless ref warning message with more info --- .../shared/reconciler/ReactCompositeComponent.js | 11 ++++++++++- .../__tests__/ReactStatelessComponent-test.js | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/renderers/shared/reconciler/ReactCompositeComponent.js b/src/renderers/shared/reconciler/ReactCompositeComponent.js index 90715744df7c2..e7f220fe1b4ba 100644 --- a/src/renderers/shared/reconciler/ReactCompositeComponent.js +++ b/src/renderers/shared/reconciler/ReactCompositeComponent.js @@ -809,7 +809,16 @@ var ReactCompositeComponentMixin = { var inst = this.getPublicInstance(); invariant(inst != null, 'Stateless function components cannot have refs.'); var publicComponentInstance = component.getPublicInstance(); - warning(publicComponentInstance != null, 'Stateless function components cannot be given refs.'); + var componentName = component && component.getName ? + component.getName() : 'a component'; + warning(publicComponentInstance != null, + 'Stateless function components cannot be given refs ' + + '(See ref "%s" in %s created by %s). ' + + 'Attempts to access this ref will fail.', + ref, + componentName, + this.getName() + ); var refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs; refs[ref] = publicComponentInstance; }, diff --git a/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js b/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js index c7db9123b151e..3f703b3cd4fa1 100644 --- a/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js +++ b/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js @@ -129,6 +129,7 @@ describe('ReactStatelessComponent', function() { spyOn(console, 'error'); var Parent = React.createClass({ + displayName: 'Parent', render: function() { return ; }, @@ -137,7 +138,9 @@ describe('ReactStatelessComponent', function() { expect(console.error.argsForCall.length).toBe(1); expect(console.error.argsForCall[0][0]).toContain( - 'Stateless function components cannot be given refs.' + 'Stateless function components cannot be given refs ' + + '(See ref "stateless" in StatelessComponent created by Parent). ' + + 'Attempts to access this ref will fail.' ); });