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 should warn when second arg isn't a function

See original GitHub issue

I’ve noticed a strange bug with the react redux forwardRef opt-in. If i use it with a connected class component, everything is ok:

const MyComponent = class Test extends React.Component {
  foo = () => console.log("Print foo from Test component");
  render() {
    return null;
  }
};

const ConnectedComponent = connect(
  null,
  null,
  null,
  { forwardRef: true }
)(MyComponent);

const store = createStore(() => {});

function App() {
  return (
    <Provider store={store}>
      <ConnectedComponent
        ref={ref => {
          if (ref) ref.foo();
        }}
      />
    </Provider>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

If i use it with a connected functional component that use forwardRef with useImperativeHandle, i obtain a strange error: create is not a function in commitHookEffectList react-dom method.

const MyComponent = React.forwardRef((props, ref) => {
  useImperativeHandle(ref, {
    foo: () => console.log("Print foo from Test component")
  });

  return null;
});

const ConnectedComponent = connect(
  null,
  null,
  null,
  { forwardRef: true }
)(MyComponent);

const store = createStore(() => {});

function App() {
  return (
    <Provider store={store}>
      <ConnectedComponent
        ref={ref => {
          if (ref) ref.foo();
        }}
      />
    </Provider>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

I create a codepen to reproduce the issue: https://codesandbox.io/s/r7rpml460o

PS: Sorry for the cors error, but i don’t find the way to add react@nextas cdn

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Jan 21, 2019

Commit log. We don’t write changelogs for alphas.

1reaction
gaearoncommented, Jan 21, 2019

Yeah, it was renamed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useImperativeHandle usage for children componenent but ...
Solution. First, update the function signature of Articles component to consume a React ref. Note that this is a second argument passed to...
Read more >
React Hooks: useRef, useImperativeHandle, & useLayoutEffect
useImperativeHandle allows you to pass values and functions from a Child component ... takes is the regular props , and the second argument...
Read more >
Hooks API Reference
The function will receive the previous value, and return an updated value. ... To implement this, pass a second argument to useEffect that...
Read more >
useImperativeHandle Hook Ultimate Guide
useImperativeHandle is the perfect hook for handling complex ref behaviors, but it is pretty complicated to understand.
Read more >
A Guide To Working With Forms And Input Fields in React
The second argument on the functional component i.e. ref will then be passed to useImperativeHandle as the first argument.
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