Skip to content

Commit 6705b39

Browse files
Fix renderer crashing with effecful hooks
1 parent 8753245 commit 6705b39

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
7676
else {
7777
let rendered;
7878

79-
let c = vnode.__c = { __v: vnode, context, props: vnode.props };
79+
let c = vnode.__c = { __v: vnode, context, props: vnode.props, __h: [] };
80+
8081
// options.render
8182
if (options.__r) options.__r(vnode);
8283

@@ -101,8 +102,6 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
101102
c.__v = vnode;
102103
// turn off stateful re-rendering:
103104
c._dirty = c.__d = true;
104-
// required by hooks
105-
c._renderCallbacks = [];
106105
c.props = props;
107106
if (c.state==null) c.state = {};
108107
c.context = cctx;

test/render.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { render, shallowRender } from '../src';
22
import { h, Component, createContext, Fragment } from 'preact';
3-
import { useState, useContext, useEffect } from 'preact/hooks';
3+
import { useState, useContext, useEffect, useLayoutEffect } from 'preact/hooks';
44
import chai, { expect } from 'chai';
55
import { spy, stub, match } from 'sinon';
66
import sinonChai from 'sinon-chai';
@@ -711,6 +711,25 @@ describe('render', () => {
711711
}).to.not.throw();
712712
});
713713

714+
it('should not crash with effectful hooks', () => {
715+
function Foo() {
716+
useEffect(() => {
717+
// Nothing
718+
}, []);
719+
720+
useLayoutEffect(() => {
721+
// Nothing
722+
}, []);
723+
724+
return <div />;
725+
}
726+
727+
// eslint-disable-next-line prefer-arrow-callback
728+
expect(function() {
729+
render(<Foo />);
730+
}).to.not.throw();
731+
});
732+
714733
it('should work with useContext and default value', () => {
715734
let Ctx = createContext('foo');
716735
function Foo() {

0 commit comments

Comments
 (0)