Skip to content

Experimenting will exposing underlying component ref for access to component methods #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/components/createConnectDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ export default function createConnectDecorator(React) {
return merged;
}

getUnderlyingRef() {
return this.underlyingRef;
}

render() {
return <DecoratedComponent {...this.merge()} />;
return <DecoratedComponent ref={component => (this.underlyingRef = component)} {...this.merge()} />;
}
};
};
Expand Down
34 changes: 34 additions & 0 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,5 +514,39 @@ describe('React', () => {

expect(decorated.DecoratedComponent).toBe(Container);
});

it('should return the instance of the wrapped component for use in calling child methods', () => {
const store = createStore(() => ({}));

const someData = {
some: 'data'
};

class Container extends Component {
someInstanceMethod() {
return someData;
}

render() {
return <div />;
}
}

const decorator = connect(state => state);
const Decorated = decorator(Container);

const tree = TestUtils.renderIntoDocument(
<Provider store={store}>
{() => (
<Decorated />
)}
</Provider>
);

const decorated = TestUtils.findRenderedComponentWithType(tree, Decorated);

expect(() => decorated.someInstanceMethod()).toThrow();
expect(decorated.getUnderlyingRef().someInstanceMethod()).toBe(someData);
});
});
});