Skip to content

Commit 9ba751c

Browse files
Fix options.diffed and options._commit not called
1 parent a3824d8 commit 9ba751c

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@ renderToString.render = renderToString;
3939
*/
4040
let shallowRender = (vnode, context) => renderToString(vnode, context, SHALLOW);
4141

42+
const EMPTY_ARR = [];
43+
function renderToString(vnode, context, opts) {
44+
const res = _renderToString(vnode, context, opts);
45+
// options._commit, we don't schedule any effects in this library right now,
46+
// so we can pass an empty queue to this hook.
47+
if (options.__c) options.__c(vnode, EMPTY_ARR);
48+
return res;
49+
}
50+
4251
/** The default export is an alias of `render()`. */
43-
function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
52+
function _renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
4453
if (vnode == null || typeof vnode === 'boolean') {
4554
return '';
4655
}
@@ -77,7 +86,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
7786
for (let i = 0; i < children.length; i++) {
7887
rendered +=
7988
(i > 0 && pretty ? '\n' : '') +
80-
renderToString(
89+
_renderToString(
8190
children[i],
8291
context,
8392
opts,
@@ -173,7 +182,8 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
173182
context = assign(assign({}, context), c.getChildContext());
174183
}
175184

176-
return renderToString(
185+
if (options.diffed) options.diffed(vnode);
186+
return _renderToString(
177187
rendered,
178188
context,
179189
opts,
@@ -313,7 +323,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
313323
: nodeName === 'foreignObject'
314324
? false
315325
: isSvgMode,
316-
ret = renderToString(
326+
ret = _renderToString(
317327
child,
318328
context,
319329
opts,

test/render.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ describe('render', () => {
10491049
});
10501050
});
10511051

1052-
it('should invoke options._render and options._diff', () => {
1052+
it('should invoke option hooks', () => {
10531053
const calls = [];
10541054
// _diff
10551055
const oldDiff = options.__b;
@@ -1063,6 +1063,17 @@ describe('render', () => {
10631063
calls.push(['_render', args]);
10641064
if (oldRender) oldRender(...args);
10651065
};
1066+
const oldDiffed = options.diffed;
1067+
options.diffed = (...args) => {
1068+
calls.push(['diffed', args]);
1069+
if (oldDiffed) oldDiffed(...args);
1070+
};
1071+
// _commit
1072+
const oldCommit = options.__c;
1073+
options.__c = (...args) => {
1074+
calls.push(['_commit', args]);
1075+
if (oldCommit) oldCommit(...args);
1076+
};
10661077

10671078
function Component1({ children }) {
10681079
return children;
@@ -1080,11 +1091,14 @@ describe('render', () => {
10801091
expect(calls).to.deep.equal([
10811092
['_diff', [vnode1]],
10821093
['_render', [vnode1]],
1094+
['diffed', [vnode1]],
10831095
['_diff', [vnode2]],
1084-
['_render', [vnode2]]
1096+
['_render', [vnode2]],
1097+
['diffed', [vnode2]],
1098+
['_commit', [vnode1, []]]
10851099
]);
10861100

1087-
expect(calls.length).to.equal(4);
1101+
expect(calls.length).to.equal(7);
10881102

10891103
options.__b = oldDiff;
10901104
options.__r = oldRender;

0 commit comments

Comments
 (0)