Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't initialize reconcile transaction on server #1870

Merged
merged 1 commit into from
Jul 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 26 additions & 3 deletions src/browser/server/__tests__/ReactServerRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,28 @@ require('mock-modules')

var mocks = require('mocks');

var ExecutionEnvironment;
var React;
var ReactMarkupChecksum;
var ReactMount;
var ReactReconcileTransaction;
var ReactTestUtils;
var ReactServerRendering;
var ReactMarkupChecksum;
var ExecutionEnvironment;

var ID_ATTRIBUTE_NAME;

describe('ReactServerRendering', function() {
beforeEach(function() {
require('mock-modules').dumpCache();
React = require('React');
ReactMarkupChecksum = require('ReactMarkupChecksum');
ReactMount = require('ReactMount');
ReactTestUtils = require('ReactTestUtils');
ReactReconcileTransaction = require('ReactReconcileTransaction');

ExecutionEnvironment = require('ExecutionEnvironment');
ExecutionEnvironment.canUseDOM = false;
ReactServerRendering = require('ReactServerRendering');
ReactMarkupChecksum = require('ReactMarkupChecksum');

var DOMProperty = require('DOMProperty');
ID_ATTRIBUTE_NAME = DOMProperty.ID_ATTRIBUTE_NAME;
Expand Down Expand Up @@ -373,5 +376,25 @@ describe('ReactServerRendering', function() {
'a valid ReactComponent.'
);
});

it('allows setState in componentWillMount without using DOM', function() {
var Component = React.createClass({
componentWillMount: function() {
this.setState({text: 'hello, world'});
},
render: function() {
return <div>{this.state.text}</div>;
}
});

ReactReconcileTransaction.prototype.perform = function() {
// We shouldn't ever be calling this on the server
throw new Error('Browser reconcile transaction should not be used');
};
var markup = ReactServerRendering.renderComponentToString(
<Component />
);
expect(markup.indexOf('hello, world') >= 0).toBe(true);
});
});
});
19 changes: 19 additions & 0 deletions src/core/ReactUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ var flushBatchedUpdates = ReactPerf.measure(
// componentDidUpdate) but we need to check here too in order to catch
// updates enqueued by setState callbacks.
while (dirtyComponents.length) {
var allUnmounted = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var allUnmounted = dirtyComponents.some((dC) => dc.isMounted()) (that should break out early).

Let's not do that this second so we can move fast, but it's so pretty and short :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of it and figured that might be slower.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for Array.some 😄

for (var i = 0, l = dirtyComponents.length; i < l; i++) {
if (dirtyComponents[i].isMounted()) {
allUnmounted = false;
break;
}
}

if (allUnmounted) {
// All the "dirty" components are unmounted, which probably means that
// they were marked dirty due to setState calls in componentWillMount
// handlers and the components are currently in the process of mounting.
// `runBatchedUpdates` will be a noop. In that case, initializing the
// DOM-dependent ReactReconcileTransaction is thus not what we want to
// do, especially when using server rendering, so we skip it.
dirtyComponents.length = 0;
return;
}

var transaction = ReactUpdatesFlushTransaction.getPooled();
transaction.perform(runBatchedUpdates, null, transaction);
ReactUpdatesFlushTransaction.release(transaction);
Expand Down