Skip to content

Commit

Permalink
(fix) - call function when ref is one (#2021)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored and marvinhagemeister committed Oct 18, 2019
1 parent 3143075 commit 48bd8b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions compat/test/browser/forwardRef.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,16 @@ describe('forwardRef', () => {
expect(ref.current==null).to.equal(true);
expect(differentRef.current.nodeName).to.equal('DIV');
});

it('calls ref when this is a function.', () => {
const spy = sinon.spy();
const Bar = forwardRef((props, ref) => {
useImperativeHandle(ref, () => ({ foo: 100 }));
return null;
});

render(<Bar ref={spy} />, scratch);
expect(spy).to.be.calledOnce;
expect(spy).to.be.calledWithExactly({ foo: 100 });
});
});
3 changes: 2 additions & 1 deletion hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export function useImperativeHandle(ref, createHandle, args) {

function bindHandles(handles) {
handles.some(handle => {
if (handle.ref) handle.ref.current = handle.createHandle();
if (typeof handle.ref === 'function') handle.ref(handle.createHandle());
else if (handle.ref) handle.ref.current = handle.createHandle();
});
return [];
}
Expand Down

0 comments on commit 48bd8b5

Please sign in to comment.