question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useImperativeHandle callback never called (when rendering w/ enzyme)

See original GitHub issue

Do you want to request a feature or report a bug? bug

What is the current behavior?

I have the code:

function Form(props, ref) {

  React.useImperativeHandle(ref, () => {
    debugger;
    return {
      setErrors: () => {},
    };
  });
}

export default React.forwardRef(Form);

When I use the component, the callback passed to useImperativeHandle is never called. (The debugger statement is never hit).

The code that I have using the component is:

import {mount} from 'enzyme';

describe('Form component', () => {
  test('exposes a ref', async () => {
    let formRef;
    mount(<Form ref={ref => (formRef = ref)}>{() => <span>test</span>}</Form>);
    await pause(500); // this is just a setTimeout to give time for the hooks to run.
    expect(formRef.setErrors).toBeDefined();
  });

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

https://codesandbox.io/s/v8rqy75mn5

What is the expected behavior?

Callback should be called and returned value should be used as the ref.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

    "react": "16.8.0-alpha.1",
    "react-dom": "16.8.0-alpha.1",
    "enzyme": "3.8.0",
    "enzyme-adapter-react-16": "1.8.0",

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Mar 7, 2019

You want:

const Form = React.forwardRef((props, ref) => {
  React.useImperativeHandle(ref, () => {
    debugger;
    return {
      setErrors: () => {},
    };
  });
})

Don’t miss React.forwardRef.

In the future might make this unnecessary but we can’t yet because that would have been a breaking change.

0reactions
threepointonecommented, Sep 4, 2019

Followed the chain and it looks like workarounds/fix existed on the enzyme side, and it’s not to do with React. Closing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useImperativeHandle callback never called (when rendering ...
(The debugger statement is never hit). The code that I have using the component is: import {mount} from 'enzyme'; describe('Form component', () ...
Read more >
React Hooks Explained: useImperativeHandle
With the help of a useEffect hook, we can call this function after the Comments component is first mounted and rendered. ... const...
Read more >
How to test React Hooks - LogRocket Blog
Testing your React app is a necessity, but testing Hooks can get a bit complicated. Learn how to test Hooks multiple ways in...
Read more >
Mock child component callback with Jest and Enzyme
You don't need to mock any component. You can use .invoke(invokePropName)(...args) => Any API invokes a function prop.
Read more >
Fix the "not wrapped in act(...)" warning when testing the ...
[0:20] To test this, we're creating a counterRef using React.createRef. We're rendering the ImperativeCounter with that counterRef, and then we verify that the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found