Skip to content

Conversation

@yungsters
Copy link
Contributor

Summary:
Currently, ReactClass mutates values returned by getDefaultProps, getInitialState, and getChildContext. This is bad because the objects may, for example, be cached and re-used across instances of a React component.

This changes ReactClass to instead create a new object. In return for allocating a new object, I've replaced mapObject with a for ... in so that we are no longer allocating an unused object.

Fair trade, IMO.

Reviewers: @zpao @sebmarkbage @spicyj

Test Plan:
Ran unit tests successfully:

npm run jest

@yungsters
Copy link
Contributor Author

I gone done a clowny; this started as #2246.

@sophiebits
Copy link
Collaborator

This changes ReactCompositeComponent to instead create a new object. In return for allocating a new object, I've replaced mapObject with forEachObject so that we are no longer allocating an unused object.

(No longer quite accurate.)

@yungsters
Copy link
Contributor Author

Ha, yeah... I just rebased. Will update the pull request summary.

@sebmarkbage
Copy link
Collaborator

It's not ideal because we could just have one new object per class/instance instead of one new per mixin. But I hate mixins so I don't care.

Summary:
Currently, `ReactClass` mutates values returned by `getDefaultProps`, `getInitialState`, and `getChildContext`. This is bad because the objects may, for example, be cached and re-used across instances of a React component.

This changes `ReactClass` to instead create a new object. In return for allocating a new object, I've replaced `mapObject` with a `for ... in` so that we are no longer allocating an unused object.

Fair trade, IMO.

Test Plan:
Ran unit tests successfully:

```
npm run jest
```

Conflicts:
	src/core/ReactCompositeComponent.js

Conflicts:
	src/class/ReactClass.js
yungsters added a commit that referenced this pull request Nov 18, 2014
Stop Mutating Merged Lifecycle Results
@yungsters yungsters merged commit ef35585 into facebook:master Nov 18, 2014
@yungsters yungsters deleted the immutable branch November 18, 2014 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants