简体   繁体   English

Typescript 另一个 Ref 的 Ref - 类型上不存在属性

[英]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.

相关问题 类型'A'.ts(2339)上不存在属性'ref' - React,TypeScript - Property 'ref' does not exist on type 'A'.ts(2339) - React, TypeScript React TypeScript 和 ForwardRef - 类型“IntrinsicAttributes”上不存在属性“ref” - React TypeScript & ForwardRef - Property 'ref' does not exist on type 'IntrinsicAttributes “IntrinsicAttributes”类型上不存在属性“ref” - Property 'ref' does not exist on type 'IntrinsicAttributes' “从不”类型上不存在属性“setTabChange”。 (React hooks Typescript) 使用 ref - Property 'setTabChange' does not exist on type 'never'. (React hooks Typescript) using ref React/Typescript - 使用 ref 时出现 typescript 错误 - 类型 '(instance: HTMLInputElement | null) =&gt; void' 上不存在属性 'current' - React/Typescript - when using ref I get typescript error - Property 'current' does not exist on type '(instance: HTMLInputElement | null) => void' 类型'IntrinsicAttributes &amp; PhoneInputProps &amp; { children?: ReactNode; 上不存在属性'ref' } - Property 'ref' does not exist on type 'IntrinsicAttributes & PhoneInputProps & { children?: ReactNode; } 在“IntrinsicAttributes”类型错误中不存在获取属性“ref” - Getting Property 'ref' does not exist on type 'IntrinsicAttributes' error forwardRef 的泛型错误:类型“IntrinsicAttributes”上不存在属性“ref” - Generics error with forwardRef: Property 'ref' does not exist on type 'IntrinsicAttributes' TypeScript掉毛错误:组件上不存在ref - TypeScript linting error: ref does not exist on component 将引用转发到 react.FC 给出类型错误:属性引用不存在于类型 &#39;IntrinsicAttributes &amp; Props &amp; { children :? 反应节点} - forwarding ref to react.FC gives type error: Property ref does not exist on type 'IntrinsicAttributes & Props & { children :? ReactNode}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM