Skip to content

Commit

Permalink
Add component prop to AltContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 17, 2015
1 parent 10fe139 commit 653cb29
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions components/mixinContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ function mixinContainer(React) {
},

altRender: function (cloneWithProps) {
var children = this.props.children

// Custom rendering function
if (typeof this.props.render === 'function') {
return this.props.render(this.getProps())
} else if (this.props.component) {
return React.createElement(this.props.component, this.getProps())
}

var children = this.props.children

// Does not wrap child in a div if we don't have to.
if (Array.isArray(children)) {
return React.createElement('div', null, children.map(function (child, i) {
Expand Down
10 changes: 10 additions & 0 deletions docs/components/altContainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,13 @@ This is a function that gets called with the props that your children will recei
```

In this example, Header, Body, and Footer will not re-render because we're returning false.

## `component`

With this prop you can define which component you want to render within your AltContainer. If you have a single component and you're using required propTypes then this is a legitimate way of rendering the components without getting a warning about invalid propTypes from React due to cloneWithProps.

```js
<AltContainer component={Body} />
```

This example renders Body inside of the AltContainer.
19 changes: 19 additions & 0 deletions test/store-listener-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,5 +483,24 @@ export default {

assert(span.props.foo.x === 888, 'when passing stores as Array they are just listened on')
},

'passing in a component as a prop'() {
const App = React.createClass({
render() {
return <strong x={this.props.x} />
}
})

const node = TestUtils.renderIntoDocument(
<AltContainer store={TestStore} component={App} />
)

const strong = TestUtils.findRenderedDOMComponentWithTag(node, 'strong')

action.sup(1337)

assert.isDefined(strong, 'component exists')
assert(strong.props.x === 1337, 'and we have props from TestStore')
},
}
}

0 comments on commit 653cb29

Please sign in to comment.