function App(){
const cntEl:any = React.useRef(null); // I don't know what type should be here.
React.useEffect(()=>{
if(cntEl.current){ cuntEl.current.start() }
}, []);
return <Countdown ref={cntEl} />
}
const Countdown = React.forwardRef((props,ref) => {
React.useImperativeHandle(ref, ()=>({
start() {
alert('Start');
}
});
return <div>Countdown</div>
});
Run Code Online (Sandbox Code Playgroud)
我尝试使用ref和在父组件中使用子方法React.useImperativeHandle()。
它运作良好。
但我不满意,因为const cntEl:any。
我相信有很多方法可以避免使用any我不知道的类型。
我只需要一个可以被替换的类型 type any。
已编辑
我可以看到(property) React.MutableRefObject<null>.current: null当我悬停在cntEl.current