Skip to content

Commit b446894

Browse files
committed
use ref callback to place instance of underlying component on wrapped component. provide wrapper method to get underlying ref to provide access to unerlying refs methods
1 parent a788376 commit b446894

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/components/createConnectDecorator.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ export default function createConnectDecorator(React) {
114114
return merged;
115115
}
116116

117+
getUnderlyingRef() {
118+
return this.underlyingRef;
119+
}
120+
117121
render() {
118-
return <DecoratedComponent {...this.props} {...this.merge()} />;
122+
return <DecoratedComponent ref={component => (this.underlyingRef = component)} {...this.props} {...this.merge()} />;
119123
}
120124
};
121125
};

test/components/connect.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,5 +514,39 @@ describe('React', () => {
514514

515515
expect(decorated.DecoratedComponent).toBe(Container);
516516
});
517+
518+
it('should return the instance of the wrapped component for use in calling child methods', () => {
519+
const store = createStore(() => ({}));
520+
521+
const someData = {
522+
some: 'data'
523+
};
524+
525+
class Container extends Component {
526+
someInstanceMethod() {
527+
return someData;
528+
}
529+
530+
render() {
531+
return <div />;
532+
}
533+
}
534+
535+
const decorator = connect(state => state);
536+
const Decorated = decorator(Container);
537+
538+
const tree = TestUtils.renderIntoDocument(
539+
<Provider store={store}>
540+
{() => (
541+
<Decorated />
542+
)}
543+
</Provider>
544+
);
545+
546+
const decorated = TestUtils.findRenderedComponentWithType(tree, Decorated);
547+
548+
expect(() => decorated.someInstanceMethod()).toThrow();
549+
expect(decorated.getUnderlyingRef().someInstanceMethod()).toBe(someData);
550+
});
517551
});
518552
});

0 commit comments

Comments
 (0)