Skip to content
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

Implement ref as an option #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions src/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function compose(dataLoader, options = {}) {
propsToWatch = null, // Watch all the props.
shouldSubscribe = null,
shouldUpdate = null,
withRef = false,
} = options;

class Container extends React.Component {
Expand Down Expand Up @@ -119,12 +120,16 @@ export default function compose(dataLoader, options = {}) {
...data,
};

const setChildRef = (c) => {
this.child = c;
};
const refs = {};
if (withRef) {
const setChildRef = (c) => {
this.child = c;
};
refs.ref = setChildRef;
}

return (
<Child ref={setChildRef} {...finalProps} />
<Child {...refs} {...finalProps} />
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/tests/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ describe('compose', () => {
});

it('should set the child ref', () => {
const options = { withRef: true };
const Container = compose((props, onData) => {
onData(null, { name: 'arunoda' });
})(Comp);
}, options)(Comp);
const el = mount(<Container name="arunoda" />);
expect(el.instance().child.props.name).to.be.equal('arunoda');
});
Expand Down