Hello @m4theushw,
I don't think that's a correct use of React.useImperativeHandle
, I tried using ref={ref}
and childRef was not null inside onBlur
. This code was already in your example, it was just commented out. I also noticed the call to inputRef.current.focus()
is unconditional, even when pressing TAB
to focus from input to button, is this expected?
I'm just going to attach docs for React.useImperativeHandle, because that's why I think your example does not correctly uses React.useImperativeHandle
Bug: `onBlur` called in wrong order if another element receives focus #23165
React version: 17.0.2
Steps To Reproduce
Link to code example: https://codesandbox.io/s/hardcore-pine-u4qn2?file=/src/App.js
The current behavior
When the button is blurred, the value of
childRef
is null inside theonBlur
callback. From my investigation, I noticed thatonBlur
is being called after unmount (causing the ref to be null). IfinputRef.current.focus()
is commented it gets called before unmount, not causing the bug. Also, forwarding the ref to an DOM element instead ofReact.useImperativeHandle
doesn't cause the bug.The expected behavior
onBlur
should be called before unmount while the ref still has a value.For more context: https://github.com/mui-org/material-ui/issues/30285