<unknown, PropsWithoutRef<P>我尝试通过Typescript和MUI更新旧的React组件:https://github.com/yuvaleros/material-ui-dropzone 但是我明白了 错误ts2345:类型'(props:nottheTheme

问题描述 投票:0回答:1
error TS2345: Argument of type '(props: WithoutTheme<P>, ref: any) => Element' is not assignable to parameter of type 'ForwardRefRenderFunction<unknown, PropsWithoutRef<P>>'. Types of parameters 'props' and 'props' are incompatible. Type 'PropsWithoutRef<P>' is not assignable to type 'WithoutTheme<P>'. Type '{ theme?: Theme | undefined; }' is not assignable to type 'WithoutTheme<P>'. return forwardRef(function ComponentWithTheme(
javascript reactjs typescript material-ui react-dropzone
1个回答
0
投票
Omit<P, "theme">

进行修改,这在此引起问题。修复此

function withTheme<P extends { theme?: Theme }, R>(
  Component: ComponentType<P & React.RefAttributes<R>>
) {
  return forwardRef<R, WithoutTheme<P>>(function ComponentWithTheme(
    props,
    ref
  ) {
    const theme = useTheme();
    const combinedProps = { ...props, theme } as P;

    return <Component ref={ref} {...combinedProps} />;
  });
}


包装的组件可以正确键入Ref(R),并且ForwardRef适当地注入道具和参考文献

    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.