From 6b7e4dc8e89e8e67794e8b52de1314cb82e7267d Mon Sep 17 00:00:00 2001 From: Ben Alpert Date: Thu, 5 Mar 2015 15:21:44 -0800 Subject: [PATCH] Add warning when using non-component element types This should make debugging easier for people who missed callsites when fixing up their JSX/non-JSX usage. --- src/core/ReactCompositeComponent.js | 14 ++++++++++++++ src/core/__tests__/ReactCompositeComponent-test.js | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index cd20f3a569..f91b3c119b 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -133,6 +133,20 @@ var ReactCompositeComponentMixin = { // Initialize the public class var inst = new Component(publicProps, publicContext); + + if (__DEV__) { + // This will throw later in _renderValidatedComponent, but add an early + // warning now to help debugging + warning( + inst.render != null, + '%s(...): No `render` method found on the returned component ' + + 'instance: you may have forgotten to define `render` in your ' + + 'component or you may have accidentally tried to render an element ' + + 'whose type is a function that isn\'t a React component.', + Component.displayName || Component.name || 'Component' + ); + } + // These should be set up in the constructor, but as a convenience for // simpler class abstractions, we set them up after the fact. inst.props = publicProps; diff --git a/src/core/__tests__/ReactCompositeComponent-test.js b/src/core/__tests__/ReactCompositeComponent-test.js index 6b8a56bb9c..c2fa177ff2 100644 --- a/src/core/__tests__/ReactCompositeComponent-test.js +++ b/src/core/__tests__/ReactCompositeComponent-test.js @@ -954,4 +954,17 @@ describe('ReactCompositeComponent', function() { expect(a).toBe(b); }); + it('should warn when using non-React functions in JSX', function() { + function NotAComponent() { + return [
,
]; + } + expect(function() { + ReactTestUtils.renderIntoDocument(
); + }).toThrow(); // has no method 'render' + expect(console.warn.calls.length).toBe(1); + expect(console.warn.calls[0].args[0]).toContain( + 'NotAComponent(...): No `render` method found' + ); + }); + });