Skip to content

Commit

Permalink
Add shouldComponentUpdate to AltContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 15, 2015
1 parent dbcd5dd commit edda162
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions components/AltContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ var AltContainer = React.createClass({
)
},

shouldComponentUpdate: function () {
return this.props.shouldComponentUpdate
? this.props.shouldComponentUpdate(this.getProps())
: true
},

render: function () {
var children = this.props.children

Expand Down
16 changes: 16 additions & 0 deletions docs/components/altContainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,19 @@ const flux = new Flux();
```

Header, Body, and Footer will have the `flux` context passed down.

## `shouldComponentUpdate`

`shouldComponentUpdate` prop allows you to fine-tune your performance needs for AltContainer only rendering when absolutely necessary.

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.

```js
<AltContainer shouldComponentUpdate={(nextProps) => false}>
<Header />
<Body />
<Footer />
</AltContainer>
```

In this example, Header, Body, and Footer will not re-render because we're returning false.
15 changes: 15 additions & 0 deletions test/store-listener-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,20 @@ export default {
assert.isObject(span.props.FooActions, 'actions are injected')
assert.isFunction(span.props.FooActions.sup, 'sup is available')
},

'scu'() {
const scu = sinon.stub().returns(true)

const node = TestUtils.renderIntoDocument(
<AltContainer shouldComponentUpdate={scu} store={TestStore}>
<span />
</AltContainer>
)

action.sup()
assert.ok(scu.calledOnce, 'custom shouldComponentUpdate was called')
assert(scu.args[0].length === 1, 'only one arg is passed, the props')
assert.isDefined(scu.args[0][0].x, 'x prop exists')
},
}
}

0 comments on commit edda162

Please sign in to comment.