1、

2、

3、

4、代码
1、index.tsx代码
import React, {useRef, useEffect} from 'react'
import MyInput from "./InputItem";
export default function Index() {
const ref = useRef<any>(null);
useEffect(() => {
ref.current?.focus();
}, [])
return (<>
<MyInput
ref={ref}
placeholder={'请输入'}
/>
</>
)
}
2、InputItem.tsx代码
import React, {useRef, forwardRef, useImperativeHandle, useEffect} from 'react'
import {Input, InputProps, InputRef} from 'antd'
type InputItemProps = InputProps & {
ref: React.RefObject<HTMLInputElement>;
}
const MyInput = forwardRef((props: InputItemProps, ref) => {
const inputRef = useRef<InputRef>();
useImperativeHandle(ref, () => {
return inputRef.current;
})
return (
<Input
{...props}
ref={inputRef}
/>
)
})
export default MyInput