Skip to content

Commit edda162

Browse files
committed
Add shouldComponentUpdate to AltContainer
1 parent dbcd5dd commit edda162

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

components/AltContainer.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ var AltContainer = React.createClass({
160160
)
161161
},
162162

163+
shouldComponentUpdate: function () {
164+
return this.props.shouldComponentUpdate
165+
? this.props.shouldComponentUpdate(this.getProps())
166+
: true
167+
},
168+
163169
render: function () {
164170
var children = this.props.children
165171

docs/components/altContainer.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,19 @@ const flux = new Flux();
197197
```
198198

199199
Header, Body, and Footer will have the `flux` context passed down.
200+
201+
## `shouldComponentUpdate`
202+
203+
`shouldComponentUpdate` prop allows you to fine-tune your performance needs for AltContainer only rendering when absolutely necessary.
204+
205+
This is a function that gets called with the props that your children will receive. You return a boolean depending on if you wish to re-render or not.
206+
207+
```js
208+
<AltContainer shouldComponentUpdate={(nextProps) => false}>
209+
<Header />
210+
<Body />
211+
<Footer />
212+
</AltContainer>
213+
```
214+
215+
In this example, Header, Body, and Footer will not re-render because we're returning false.

test/store-listener-component-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,5 +461,20 @@ export default {
461461
assert.isObject(span.props.FooActions, 'actions are injected')
462462
assert.isFunction(span.props.FooActions.sup, 'sup is available')
463463
},
464+
465+
'scu'() {
466+
const scu = sinon.stub().returns(true)
467+
468+
const node = TestUtils.renderIntoDocument(
469+
<AltContainer shouldComponentUpdate={scu} store={TestStore}>
470+
<span />
471+
</AltContainer>
472+
)
473+
474+
action.sup()
475+
assert.ok(scu.calledOnce, 'custom shouldComponentUpdate was called')
476+
assert(scu.args[0].length === 1, 'only one arg is passed, the props')
477+
assert.isDefined(scu.args[0][0].x, 'x prop exists')
478+
},
464479
}
465480
}

0 commit comments

Comments
 (0)