Skip to content

Commit 7444288

Browse files
marvinhagemeisterJoviDeCroock
authored andcommitted
Initialize component state as an empty object (#119)
1 parent 5839626 commit 7444288

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
9898
// turn off stateful re-rendering:
9999
c._dirty = c.__d = true;
100100
c.props = props;
101+
if (c.state==null) c.state = {};
101102
c.context = context;
102103
if (nodeName.getDerivedStateFromProps) c.state = assign(assign({}, c.state), nodeName.getDerivedStateFromProps(c.props, c.state));
103104
else if (c.componentWillMount) c.componentWillMount();

test/render.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ describe('render', () => {
253253
.to.have.been.calledOnce
254254
.and.calledWithExactly(
255255
match(PROPS),
256-
match.falsy,
256+
match({}),
257257
match({}) // empty context
258258
);
259259
});
@@ -285,7 +285,7 @@ describe('render', () => {
285285
foo: 1,
286286
children: match({ type: 'span', props: { children: 'asdf' } })
287287
}),
288-
match.falsy,
288+
match({}),
289289
match({})
290290
);
291291
});
@@ -307,6 +307,19 @@ describe('render', () => {
307307
expect(render(<Test foo={undefined} bar="b" />), 'overridden').to.equal('<div foo="default foo" bar="b"></div>');
308308
});
309309

310+
it('should initialize state as an empty object', () => {
311+
const fn = spy();
312+
class Test extends Component {
313+
render(state) {
314+
fn(this.state, state);
315+
return <div />;
316+
}
317+
}
318+
319+
render(<Test />);
320+
expect(fn).to.be.calledOnce.and.be.calledWith(match({}), match({}));
321+
});
322+
310323
it('should invoke getDerivedStateFromProps', () => {
311324
class Test extends Component {
312325
static getDerivedStateFromProps() {}
@@ -386,13 +399,13 @@ describe('render', () => {
386399
render(<Outer />);
387400

388401
expect(Outer.prototype.getChildContext).to.have.been.calledOnce;
389-
expect(Inner.prototype.render).to.have.been.calledWith(match({}), match.falsy, CONTEXT);
402+
expect(Inner.prototype.render).to.have.been.calledWith(match({}), match({}), CONTEXT);
390403

391404
CONTEXT.foo = 'bar';
392405
render(<Outer {...PROPS} />);
393406

394407
expect(Outer.prototype.getChildContext).to.have.been.calledTwice;
395-
expect(Inner.prototype.render).to.have.been.calledWith(match(PROPS), match.falsy, CONTEXT);
408+
expect(Inner.prototype.render).to.have.been.calledWith(match(PROPS), match({}), CONTEXT);
396409
});
397410

398411
it('should pass context to direct children', () => {
@@ -419,13 +432,13 @@ describe('render', () => {
419432
render(<Outer />);
420433

421434
expect(Outer.prototype.getChildContext).to.have.been.calledOnce;
422-
expect(Inner.prototype.render).to.have.been.calledWith(match({}), match.falsy, CONTEXT);
435+
expect(Inner.prototype.render).to.have.been.calledWith(match({}), match({}), CONTEXT);
423436

424437
CONTEXT.foo = 'bar';
425438
render(<Outer {...PROPS} />);
426439

427440
expect(Outer.prototype.getChildContext).to.have.been.calledTwice;
428-
expect(Inner.prototype.render).to.have.been.calledWith(match(PROPS), match.falsy, CONTEXT);
441+
expect(Inner.prototype.render).to.have.been.calledWith(match(PROPS), match({}), CONTEXT);
429442

430443
// make sure render() could make use of context.a
431444
expect(Inner.prototype.render).to.have.returned(match({ props: { children: 'a' } }));
@@ -463,8 +476,8 @@ describe('render', () => {
463476

464477
render(<Outer />);
465478

466-
expect(Inner.prototype.render).to.have.been.calledWith(match({}), match.falsy, { outerContext });
467-
expect(InnerMost.prototype.render).to.have.been.calledWith(match({}), match.falsy, { outerContext, innerContext });
479+
expect(Inner.prototype.render).to.have.been.calledWith(match({}), match({}), { outerContext });
480+
expect(InnerMost.prototype.render).to.have.been.calledWith(match({}), match({}), { outerContext, innerContext });
468481
});
469482
});
470483

0 commit comments

Comments
 (0)