Skip to content

Commit b6e1eb2

Browse files
awearygaearon
authored andcommitted
Inject default batching after pending transactions (#7033)
1 parent 1a0e3a3 commit b6e1eb2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/renderers/dom/server/ReactServerRendering.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var emptyObject = require('emptyObject');
2525
var instantiateReactComponent = require('instantiateReactComponent');
2626
var invariant = require('invariant');
2727

28+
var pendingTransactions = 0;
29+
2830
/**
2931
* @param {ReactElement} element
3032
* @return {string} the HTML markup
@@ -36,6 +38,8 @@ function renderToStringImpl(element, makeStaticMarkup) {
3638

3739
transaction = ReactServerRenderingTransaction.getPooled(makeStaticMarkup);
3840

41+
pendingTransactions++;
42+
3943
return transaction.perform(function() {
4044
var componentInstance = instantiateReactComponent(element, true);
4145
var markup = ReactReconciler.mountComponent(
@@ -56,10 +60,15 @@ function renderToStringImpl(element, makeStaticMarkup) {
5660
return markup;
5761
}, null);
5862
} finally {
63+
pendingTransactions--;
5964
ReactServerRenderingTransaction.release(transaction);
6065
// Revert to the DOM batching strategy since these two renderers
6166
// currently share these stateful modules.
62-
ReactUpdates.injection.injectBatchingStrategy(ReactDefaultBatchingStrategy);
67+
if (!pendingTransactions) {
68+
ReactUpdates.injection.injectBatchingStrategy(
69+
ReactDefaultBatchingStrategy
70+
);
71+
}
6372
}
6473
}
6574

src/renderers/dom/server/__tests__/ReactServerRendering-test.js

+31
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,37 @@ describe('ReactServerRendering', function() {
399399
);
400400
expect(markup.indexOf('hello, world') >= 0).toBe(true);
401401
});
402+
403+
it('renders components with different batching strategies', function() {
404+
var StaticComponent = React.createClass({
405+
render: function() {
406+
const staticContent = ReactServerRendering.renderToStaticMarkup(
407+
<div>
408+
<img src="foo-bar.jpg" />
409+
</div>
410+
);
411+
return <div dangerouslySetInnerHTML={{__html: staticContent}} />;
412+
},
413+
});
414+
415+
var Component = React.createClass({
416+
componentWillMount: function() {
417+
this.setState({text: 'hello, world'});
418+
},
419+
render: function() {
420+
return <div>{this.state.text}</div>;
421+
},
422+
});
423+
expect(
424+
ReactServerRendering.renderToString.bind(
425+
ReactServerRendering,
426+
<div>
427+
<StaticComponent />
428+
<Component />
429+
</div>
430+
)
431+
).not.toThrow();
432+
});
402433
});
403434

404435
it('warns with a no-op when an async setState is triggered', function() {

0 commit comments

Comments
 (0)