useImperativeHandle should warn when second arg isn't a function
See original GitHub issueI’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@next
as cdn
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Commit log. We don’t write changelogs for alphas.
Yeah, it was renamed.