Skip to content

Commit 5ba648c

Browse files
committed
Invoke componentWillMount() for stateful components just before render(). Fixes #6. Thanks @mikestead!
1 parent 6194604 commit 5ba648c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export default function renderToString(vnode, context, opts, inner) {
152152
let c = new nodeName(props, context);
153153
c.props = props;
154154
c.context = context;
155+
if (c.componentWillMount) c.componentWillMount();
155156
rendered = c.render(c.props, c.state, c.context);
156157

157158
if (c.getChildContext) {

test/render.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,23 @@ describe('render', () => {
212212
expect(render(<Test bar="b" />), 'partial').to.equal('<div foo="default foo" bar="b"></div>');
213213
expect(render(<Test foo="a" bar="b" />), 'overridden').to.equal('<div foo="a" bar="b"></div>');
214214
});
215+
216+
it('should invoke componentWillMount', () => {
217+
class Test extends Component {
218+
componentWillMount() {}
219+
render(props) {
220+
return <div {...props} />;
221+
}
222+
}
223+
spy(Test.prototype, 'componentWillMount');
224+
spy(Test.prototype, 'render');
225+
226+
render(<Test />);
227+
228+
expect(Test.prototype.componentWillMount)
229+
.to.have.been.calledOnce
230+
.and.to.have.been.calledBefore(Test.prototype.render);
231+
});
215232
});
216233

217234
describe('High-order components', () => {

0 commit comments

Comments
 (0)