-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This provides an easy way to indicate that components should only rerender when given new props, like PureRenderMixin. If you rely on mutation in your React components, you can continue to use `React.Component`. Inheriting from `React.PureComponent` indicates to React that your component doesn't need to rerender when the props are unchanged. We'll compare the old and new props before each render and short-circuit if they're unchanged. It's like an automatic shouldComponentUpdate.
- Loading branch information
1 parent
0d892c0
commit c8fbdac
Showing
4 changed files
with
219 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule ReactPureComponent | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var ReactComponent = require('ReactComponent'); | ||
var ReactNoopUpdateQueue = require('ReactNoopUpdateQueue'); | ||
|
||
var emptyObject = require('emptyObject'); | ||
|
||
/** | ||
* Base class helpers for the updating state of a component. | ||
*/ | ||
function ReactPureComponent(props, context, updater) { | ||
// Duplicated from ReactComponent. | ||
this.props = props; | ||
this.context = context; | ||
this.refs = emptyObject; | ||
// We initialize the default updater but the real one gets injected by the | ||
// renderer. | ||
this.updater = updater || ReactNoopUpdateQueue; | ||
} | ||
|
||
function ComponentDummy() {} | ||
ComponentDummy.prototype = ReactComponent.prototype; | ||
ReactPureComponent.prototype = new ComponentDummy(); | ||
ReactPureComponent.prototype.constructor = ReactPureComponent; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
churchie317
Contributor
|
||
// Avoid an extra prototype jump for these methods. | ||
Object.assign(ReactPureComponent.prototype, ReactComponent.prototype); | ||
ReactPureComponent.prototype.isPureReactComponent = true; | ||
|
||
module.exports = ReactPureComponent; |
97 changes: 97 additions & 0 deletions
97
src/isomorphic/modern/class/__tests__/ReactPureComponent-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React; | ||
var ReactDOM; | ||
|
||
describe('ReactPureComponent', function() { | ||
beforeEach(function() { | ||
React = require('React'); | ||
ReactDOM = require('ReactDOM'); | ||
}); | ||
|
||
it('should render', function() { | ||
var renders = 0; | ||
class Component extends React.PureComponent { | ||
constructor() { | ||
super(); | ||
this.state = {type: 'mushrooms'}; | ||
} | ||
render() { | ||
renders++; | ||
return <div>{this.props.text[0]}</div>; | ||
} | ||
} | ||
|
||
var container = document.createElement('div'); | ||
var text; | ||
var component; | ||
|
||
text = ['porcini']; | ||
component = ReactDOM.render(<Component text={text} />, container); | ||
expect(container.textContent).toBe('porcini'); | ||
expect(renders).toBe(1); | ||
|
||
text = ['morel']; | ||
component = ReactDOM.render(<Component text={text} />, container); | ||
expect(container.textContent).toBe('morel'); | ||
expect(renders).toBe(2); | ||
|
||
text[0] = 'portobello'; | ||
component = ReactDOM.render(<Component text={text} />, container); | ||
expect(container.textContent).toBe('morel'); | ||
expect(renders).toBe(2); | ||
|
||
// Setting state without changing it doesn't cause a rerender. | ||
component.setState({type: 'mushrooms'}); | ||
expect(container.textContent).toBe('morel'); | ||
expect(renders).toBe(2); | ||
|
||
// But changing state does. | ||
component.setState({type: 'portobello mushrooms'}); | ||
expect(container.textContent).toBe('portobello'); | ||
expect(renders).toBe(3); | ||
}); | ||
|
||
it('can override shouldComponentUpdate', function() { | ||
var renders = 0; | ||
class Component extends React.PureComponent { | ||
render() { | ||
renders++; | ||
return <div />; | ||
} | ||
shouldComponentUpdate() { | ||
return true; | ||
} | ||
} | ||
var container = document.createElement('div'); | ||
ReactDOM.render(<Component />, container); | ||
ReactDOM.render(<Component />, container); | ||
expect(renders).toBe(2); | ||
}); | ||
|
||
it('extends React.Component', function() { | ||
var renders = 0; | ||
class Component extends React.PureComponent { | ||
render() { | ||
expect(this instanceof React.Component).toBe(true); | ||
expect(this instanceof React.PureComponent).toBe(true); | ||
renders++; | ||
return <div />; | ||
} | ||
} | ||
ReactDOM.render(<Component />, document.createElement('div')); | ||
expect(renders).toBe(1); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isn't this line going to be overridden by the next one? Or doesn't ReactComponent.prototype.constructor exist?