Skip to content

Commit

Permalink
Inject default batching after pending transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Dail authored and Brandon Dail committed Jun 13, 2016
1 parent e487c36 commit a0eb88d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/renderers/dom/server/ReactServerRendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var emptyObject = require('emptyObject');
var instantiateReactComponent = require('instantiateReactComponent');
var invariant = require('invariant');

var pendingTransactions = 0;

/**
* @param {ReactElement} element
* @return {string} the HTML markup
Expand All @@ -36,6 +38,8 @@ function renderToStringImpl(element, makeStaticMarkup) {

transaction = ReactServerRenderingTransaction.getPooled(makeStaticMarkup);

pendingTransactions++;

return transaction.perform(function() {
if (__DEV__) {
ReactInstrumentation.debugTool.onBeginFlush();
Expand All @@ -60,10 +64,15 @@ function renderToStringImpl(element, makeStaticMarkup) {
return markup;
}, null);
} finally {
pendingTransactions--;
ReactServerRenderingTransaction.release(transaction);
// Revert to the DOM batching strategy since these two renderers
// currently share these stateful modules.
ReactUpdates.injection.injectBatchingStrategy(ReactDefaultBatchingStrategy);
if (!pendingTransactions) {
ReactUpdates.injection.injectBatchingStrategy(
ReactDefaultBatchingStrategy
);
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/renderers/dom/server/__tests__/ReactServerRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,38 @@ describe('ReactServerRendering', function() {
);
expect(markup.indexOf('hello, world') >= 0).toBe(true);
});

it('allows setState in component rendered after another using renderToString',
function() {
var StaticComponent = React.createClass({
render: function() {
const staticContent = ReactServerRendering.renderToStaticMarkup(
<div>
<img src="foo-bar.jpg" />
</div>
);
// const staticContent = '<h1>Hello</h1>'
return <div dangerouslySetInnerHTML={{__html: staticContent}} />;
},
});

var Component = React.createClass({
componentWillMount: function() {
this.setState({text: 'hello, world'});
},
render: function() {
return <div>{this.state.text}</div>;
},
});
expect(
ReactServerRendering.renderToString.bind(
ReactServerRendering,
<div>
<StaticComponent />
<Component />
</div>
)
).not.toThrow();
});
});
});

0 comments on commit a0eb88d

Please sign in to comment.