import { useRef } from 'react'
import { Input } from 'antd'
const ref = useRef<Input>()
我用上面的方式,不能把字符串赋为初始值,使用如下的方式
const ref = useRef<Input>('init')
请问该如何正确的设置类型和初始值呢?
import { useRef } from 'react'
import { Input } from 'antd'
const ref = useRef<Input>()
我用上面的方式,不能把字符串赋为初始值,使用如下的方式
const ref = useRef<Input>('init')
请问该如何正确的设置类型和初始值呢?
useRef初始值是Input引用的初始值,不是Input组件的value属性的初始值
非要使用可以 useRef<Input>(new Input({value: "init"})), 不过这样太奇怪了
如果提示Object is possible null/undefined, 可以先判断下 :
if (ref.current) { ref.current.focus() }
Typescript就不会报错了
亲测可用
import type { InputRef } from 'antd';
const ref = useRef<InputRef>(null);
2 回答1k 阅读✓ 已解决
2 回答2.6k 阅读
3 回答740 阅读
1 回答800 阅读