【发布时间】: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 ....
<>)
}
但是对于新版本,现在我的代码在 <ActionSheet> 内的引用上出现 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