[英]Typescript Ref of another Ref - Property does not on exist on type
I'm using https://github.com/lhz516/react-h5-audio-player and I am attempting to correctly declare my useRef
type without just using useRef<any>(null)
.我正在使用https://github.com/lhz516/react-h5-audio-player并且我试图在不使用
useRef<any>(null)
的情况下正确声明我的useRef
类型。
I can access and update the audio element by using the following (the functionality is to move the recording to a section when you click on the corresponding text transcription).我可以使用以下方法访问和更新音频元素(功能是当您单击相应的文本转录时将录音移动到一个部分)。
const playerRef = useRef<any>(null);
const updateCurrentTime = useCallback((aTime: number) => {
if (playerRef.current !== null) {
playerRef.current.audio.current.currentTime = aTime;
}
}, [playerRef]);
return (
<div>
<div>
<ReactAudioPlayer
ref={playerRef}
{...playerProps}
/>
</div>
<div>
{props.text.map((aText, aIndex) => (
<span
key={aIndex}
onClick={() => updateCurrentTime(aText.start)}>
{aText.value}
</span>))}
<div>
</div>
)
I tried to do this:我试着这样做:
const playerRef = useRef<MutableRefObject<ReactAudioPlayer>>(null);
But I get an error:但我得到一个错误:
Property 'audio' does not exist on type 'MutableRefObject<H5AudioPlayer>'. ts(2329)
I tried playing around and doing an interface but I think I'm missing something as it doesn't change the error message:我试着玩弄并做一个界面,但我想我遗漏了一些东西,因为它不会改变错误信息:
interface ReactAudioPlayerRef {
audio : RefObject<ReactAudioPlayer> | MutableRefObject<ReactAudioPlayer> | ReactAudioPlayer
}
const playerRef = useRef<MutableRefObject<ReactAudioPlayerRef>>(null); // Same error message
Is there a way I can handle this error message without just using the any
type?有没有一种方法可以在不使用
any
类型的情况下处理此错误消息?
by reading this code: https://github.com/lhz516/react-h5-audio-player/blob/master/src/index.tsx通过阅读这段代码: https://github.com/lhz516/react-h5-audio-player/blob/master/src/index.tsx
i found that they use a ref with HTMLAudioElement for audio我发现他们将 ref 与 HTMLAudioElement 一起用于音频
audio = createRef<HTMLAudioElement>()
did u try this?你试过这个吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.