diff --git a/src/renderers/shared/stack/reconciler/ReactRef.js b/src/renderers/shared/stack/reconciler/ReactRef.js index a7350cc6a8a73..64cfd15af6894 100644 --- a/src/renderers/shared/stack/reconciler/ReactRef.js +++ b/src/renderers/shared/stack/reconciler/ReactRef.js @@ -13,6 +13,7 @@ 'use strict'; var ReactOwner = require('ReactOwner'); +var warning = require('warning'); import type { ReactInstance } from 'ReactInstanceType'; import type { ReactElement } from 'ReactElementType'; @@ -21,7 +22,12 @@ var ReactRef = {}; function attachRef(ref, component, owner) { if (typeof ref === 'function') { - ref(component.getPublicInstance()); + var instance = component.getPublicInstance(); + warning( + instance !== null, + 'Stateless function components cannot have refs.' + ); + ref(instance); } else { // Legacy ref ReactOwner.addComponentAsRefTo( diff --git a/src/renderers/shared/stack/reconciler/__tests__/ReactStatelessComponent-test.js b/src/renderers/shared/stack/reconciler/__tests__/ReactStatelessComponent-test.js index 59d1e117ed6ae..7139c085aeb53 100644 --- a/src/renderers/shared/stack/reconciler/__tests__/ReactStatelessComponent-test.js +++ b/src/renderers/shared/stack/reconciler/__tests__/ReactStatelessComponent-test.js @@ -149,9 +149,9 @@ describe('ReactStatelessComponent', () => { it('should warn for functional refs in pure functions', function() { spyOn(console, 'error'); function Child() { - return
{}} />; + return
; } - ReactTestUtils.renderIntoDocument(); + ReactTestUtils.renderIntoDocument( {}} />); expect(console.error.calls.count()).toBe(1); expect(console.error.calls.argsFor(0)[0]).toContain( 'Stateless function components cannot have refs.'