【问题标题】:React createRef not assignable to type MutableRefObject in React Native Actions Sheet ref propReact createRef 不可分配给 React Native Actions Sheet ref prop 中的 MutableRefObject 类型
【发布时间】:2025-11-21 12:00:01
【问题描述】:

我正在使用最近添加了 Typescript 支持的 react-native-actions-sheet v0.5.1:

export default function MyFunc(){
   const actionSheetRef = createRef<ActionSheet>()
   useEffect(() => {
        actionSheetRef.current?.setModalVisible(props.open)
   }, [props.open])
   return (
        <>
            <ActionSheet
                ref={actionSheetRef}
                onClose={props.onClose}
            >
            // more code here  ....
        <>)
}

但是对于新版本,现在我的代码在 &lt;ActionSheet&gt; 内的引用上出现 Typescript 错误:

Type 'RefObject<ActionSheet>' is not assignable to type '(string & MutableRefObject<{ setModalVisible(visible?: boolean | undefined): void; show(): void; hide(): void; handleChildScrollEnd(): void; snapToOffset(offset: number): void; }>) | (RefObject<...> & MutableRefObject<...>) | (((instance: ActionSheet | null) => void) & MutableRefObject<...>) | undefined'.
  Type 'RefObject<ActionSheet>' is not assignable to type '((instance: ActionSheet | null) => void) & MutableRefObject<{ setModalVisible(visible?: boolean | undefined): void; show(): void; hide(): void; handleChildScrollEnd(): void; snapToOffset(offset: number): void; }>'.
    Type 'RefObject<ActionSheet>' is not assignable to type '(instance: ActionSheet | null) => void'.
      Type 'RefObject<ActionSheet>' provides no match for the signature '(instance: ActionSheet | null): void'.

知道怎么解决吗?

【问题讨论】:

  • 而不是createRef,当您使用useRef 时会发生什么?
  • 嗯,我根本无法重现这个。您的代码示例有效(在整理了不相关的错误之后)为我通过了类型检查。

标签: reactjs typescript react-native react-native-actions-sheet


【解决方案1】:

软件包开发人员已发布修复此问题的更新 (see GitHub issue)。您可以使用此代码:

const actionSheetRef = useRef<ActionSheet>(null)
<ActionSheet
            ref={actionSheetRef}
            onClose={props.handleClose}
        >
   ...
</ActionSheet>

【讨论】: