forward ref 的对应项,useImperativeMethods 自定义使用 ref 时公开给父组件的实例值。useImperativeMethods 应与 forwardRef 一起使用:
import { useImperativeHandle, useRef, forwardRef } from 'rax';
function FancyInput(props, ref) {
const inputRef = useRef();
useImperativeHandle(ref, () => ({
focus: () => {
inputRef.current.focus();
}
}));
return <input ref={inputRef} />;
}
FancyInput = forwardRef(FancyInput);