我试图让我的组件接受一个引用。
我有一个像这样的组件:
const MyComponent: RefForwardingComponent<HTMLDivElement, IMyComponentProps> = (props, ref) => {
return <div ref={ref}>Hoopla</div>
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将 ref 传递给时是这样的:
<MyComponent ref={myRef}></MyComponent>
Run Code Online (Sandbox Code Playgroud)
...我收到此错误:
Property 'ref' does not exist on type 'IntrinsicAttributes & IMyComponentProps & { children?: ReactNode; }'.ts(2322)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在尝试解决图像滑块中的一些性能问题,我发现使用它animated.img比使用内部的某些反应组件产生更好的性能animated.div。
React 组件显然不仅仅是为了它的乐趣而放置在里面,但幸运的是,react-spring 允许您通过执行以下操作来为自定义组件设置动画
const AnimatedComponent = animated(Component)
Run Code Online (Sandbox Code Playgroud)
根据文档
但我该如何使用它呢?我一直在尝试,但 Typescript 只是给出了一些关于缺少 269 种不同类型的道具的非常无用的信息。
编辑添加错误
vscode 显示了 typescript 错误,但这可能并不重要。由于我不知道要传递哪些道具才能为组件设置动画,因此我对它不起作用并不感到惊讶,但错误消息并不能真正帮助确定我需要做什么。
' is missing the following properties from type 'AnimatedProps<{ title: string | FluidValue<string, any>; id: string | FluidValue<string, any>; article?: { title: string; metaTitle: string; metaDescription: string; description: string; showRelatedArticles: boolean; elements: ({ ...; } | ... 4 more ... | { ...; })[]; } | null | undefined; ... 269 more ...; key?: st...': …Run Code Online (Sandbox Code Playgroud)