Skip to content

Commit bb33e65

Browse files
committed
Pass parent props into mapStateToProps
Addresses part of #52 by passing the parent props into mapStateWithProps.
1 parent d13d76a commit bb33e65

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/components/createConnect.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export default function createConnect(React) {
3434
// Helps track hot reloading.
3535
const version = nextVersion++;
3636

37-
function computeStateProps(store) {
37+
function computeStateProps(store, props) {
3838
const state = store.getState();
39-
const stateProps = finalMapStateToProps(state);
39+
const stateProps = finalMapStateToProps(state, props);
4040
invariant(
4141
isPlainObject(stateProps),
4242
'`mapStateToProps` must return an object. Instead received %s.',
@@ -95,15 +95,15 @@ export default function createConnect(React) {
9595
`or explicitly pass "store" as a prop to "${this.constructor.displayName}".`
9696
);
9797

98-
this.stateProps = computeStateProps(this.store);
98+
this.stateProps = computeStateProps(this.store, props);
9999
this.dispatchProps = computeDispatchProps(this.store);
100100
this.state = {
101101
props: this.computeNextState()
102102
};
103103
}
104104

105-
recomputeStateProps() {
106-
const nextStateProps = computeStateProps(this.store);
105+
recomputeStateProps(props = this.props) {
106+
const nextStateProps = computeStateProps(this.store, props);
107107
if (shallowEqual(nextStateProps, this.stateProps)) {
108108
return false;
109109
}
@@ -163,6 +163,7 @@ export default function createConnect(React) {
163163

164164
componentWillReceiveProps(nextProps) {
165165
if (!shallowEqual(nextProps, this.props)) {
166+
this.stateProps = computeStateProps(this.store, nextProps);
166167
this.recomputeState(nextProps);
167168
}
168169
}

test/components/connect.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,45 @@ describe('React', () => {
375375
expect(div.props.stateThing).toBe('HELLO azbzcZ');
376376
});
377377

378+
it('should pass state and parent props to mapStateToProps', () => {
379+
const store = createStore(stringBuilder);
380+
381+
@connect(
382+
(state, props) => ({idString: `[${props.id}] ${state}`}),
383+
)
384+
class Container extends Component {
385+
render() {
386+
return <div {...this.props}/>;
387+
};
388+
}
389+
390+
class OuterContainer extends Component {
391+
constructor() {
392+
super();
393+
this.state = {id: 0};
394+
}
395+
396+
render() {
397+
return (
398+
<Provider store={store}>
399+
{() => <Container id={this.state.id} />}
400+
</Provider>
401+
);
402+
}
403+
}
404+
405+
const tree = TestUtils.renderIntoDocument(<OuterContainer />);
406+
const div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
407+
408+
expect(div.props.idString).toBe('[0] ');
409+
store.dispatch({type: 'APPEND', body: 'a'});
410+
expect(div.props.idString).toBe('[0] a');
411+
tree.setState({id: 1});
412+
expect(div.props.idString).toBe('[1] a');
413+
store.dispatch({type: 'APPEND', body: 'b'});
414+
expect(div.props.idString).toBe('[1] ab');
415+
});
416+
378417
it('should merge actionProps into WrappedComponent', () => {
379418
const store = createStore(() => ({
380419
foo: 'bar'

0 commit comments

Comments
 (0)