typescript 类型...无法指派给类型'IntrinsicAttributes & CodeLinkContentRightHandSideProps'

8ehkhllq  于 2022-11-18  发布在  TypeScript
关注(0)|答案(1)|浏览(172)

正在尝试处理React/TS应用中某个组件的某些鼠标事件。不确定将鼠标事件放在何处或如何将其/已处理的事件从子道具向上传递回父道具。
这是代码:

const [isMouseOver, setIsMouseOver] = useState(false)

 <ContentRightHandSide
            onMouseEnter={() => setIsMouseOver(true)}
            onMouseLeave={() => setIsMouseOver(false)}
          > 
           [...]
    </ContentRightHandSide>

这是ContentRightHandSide组件的代码

type ContentRightHandSideProps = {
  children?: ReactNode;
};

export function ContentRightHandSide({
  children,
  offset,
}: ContentRightHandSideProps) {
  return (
    <div>
      {children}
    </div>
  );
}

这是错误:

(property) onMouseEnter: () => any
Type '{ children: Element[]; offset: number; onMouseEnter: () => any; onMouseLeave: () => any; }' 
is not assignable to type 'IntrinsicAttributes & ContentRightHandSideProps'.
Property 'onMouseEnter' does not exist on type 'IntrinsicAttributes & ContentRightHandSideProps'

我将非常感谢任何帮助提供,谢谢!

izj3ouym

izj3ouym1#

无法在自定义组件上调用事件,除非在属性中定义了该事件

type ContentRightHandSideProps = {
  children?: ReactNode; 
  offset: string,
  onMouseEnter: (e: MouseEvent<HTMLButtonElement>) => void,
  onMouseLeave: (e: MouseEvent<HTMLButtonElement>) => void,
};

export function ContentRightHandSide({
  children,
  offset,
  onMouseEnter,
  onMouseLeave
}: ContentRightHandSideProps) {
  return (
    <div  onMouseEnter={onMouseEnter}
            onMouseLeave={onMouseLeave}>
      {children}
    </div>
  );
}

相关问题