Skip to content

Commit fa032e5

Browse files
author
jim
committed
Enable module pattern.
1 parent 4a1b0b7 commit fa032e5

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/renderers/shared/reconciler/ReactCompositeComponent.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ function StatelessComponent(Component) {
4444
StatelessComponent.prototype.render = function() {
4545
var Component = ReactInstanceMap.get(this)._currentElement.type;
4646
var element = Component(this.props, this.context, this.updater);
47+
warnIfInvalidElement(Component, element);
48+
return element;
49+
};
50+
51+
function warnIfInvalidElement(Component, element) {
4752
if (__DEV__) {
4853
warning(
4954
element === null || element === false || ReactElement.isValidElement(element),
50-
'%s must be a class extending React.Component or be a stateless ' +
51-
'function that returns a valid React element.',
55+
'%s(...): A valid React element (or null) must be returned. You may have ' +
56+
'returned undefined, an array or some other invalid object.',
5257
Component.displayName || Component.name || 'Component'
5358
);
5459
}
55-
return element;
56-
};
60+
}
5761

5862
/**
5963
* ------------------ The Life-Cycle of a Composite Component ------------------
@@ -168,7 +172,29 @@ var ReactCompositeComponentMixin = {
168172
inst = new Component(publicProps, publicContext, ReactUpdateQueue);
169173
}
170174
} else {
171-
inst = new StatelessComponent(Component);
175+
if (__DEV__) {
176+
ReactCurrentOwner.current = this;
177+
try {
178+
inst = Component(publicProps, publicContext, ReactUpdateQueue);
179+
} finally {
180+
ReactCurrentOwner.current = null;
181+
}
182+
} else {
183+
inst = Component(publicProps, publicContext, ReactUpdateQueue);
184+
}
185+
if (inst == null || inst.render == null) {
186+
renderedElement = inst;
187+
warnIfInvalidElement(Component, renderedElement);
188+
invariant(
189+
inst === null ||
190+
inst === false ||
191+
ReactElement.isValidElement(inst),
192+
'%s(...): A valid React element (or null) must be returned. You may have ' +
193+
'returned undefined, an array or some other invalid object.',
194+
Component.displayName || Component.name || 'Component'
195+
);
196+
inst = new StatelessComponent(Component);
197+
}
172198
}
173199

174200
if (__DEV__) {
@@ -869,7 +895,7 @@ var ReactCompositeComponentMixin = {
869895
// TODO: An `isValidNode` function would probably be more appropriate
870896
renderedComponent === null || renderedComponent === false ||
871897
ReactElement.isValidElement(renderedComponent),
872-
'%s.render(): A valid ReactComponent must be returned. You may have ' +
898+
'%s.render(): A valid React element (or null) must be returned. You may have ' +
873899
'returned undefined, an array or some other invalid object.',
874900
this.getName() || 'ReactCompositeComponent'
875901
);

src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ describe('ReactCompositeComponent', function() {
7272
spyOn(console, 'error');
7373
});
7474

75+
it('should support module pattern components', function() {
76+
function Child({test}) {
77+
return {
78+
render() {
79+
return <div>{test}</div>;
80+
},
81+
};
82+
}
83+
84+
var el = document.createElement('div');
85+
ReactDOM.render(<Child test="test" />, el);
86+
87+
expect(el.textContent).toBe('test');
88+
});
89+
7590
it('should support rendering to different child types over time', function() {
7691
var instance = <MorphingComponent />;
7792
instance = ReactTestUtils.renderIntoDocument(instance);

0 commit comments

Comments
 (0)