简体   繁体   English

Typescript React - 使用 MouseEvents 而不将其传递给子元素

[英]Typescript React - use MouseEvents without passing it to child element

Can I use Mouse Events on whole React Element or I have to pass it to child element to get it work?我可以在整个 React 元素上使用鼠标事件还是必须将其传递给子元素才能使其工作? After several functions components where I passed my handleEvent function I want to know if it's possible without getting a TypeScript error.在我传递了我的handleEvent函数的几个函数组件之后,我想知道是否有可能没有出现TypeScript错误。 My code is really simple我的代码真的很简单

<Tile onHover={handleHover} name="Random name"/>

As your Title component does not expect an onHover prop, it will just be ignored.由于您的Title组件不需要onHover道具,它只会被忽略。 The event you want to use is rather onMouseOver , which works on most HTML elements ( according to documentation ):您要使用的事件是onMouseOver ,它适用于大多数 HTML 元素(根据文档):

All HTML elements, EXCEPT: <base> , <bdo> , <br> , <head> , <html> , <iframe> , <meta> , <param> , <script> , <style> , and <title>所有 HTML 元素,除了: <base><bdo><br><head><html><iframe><meta><param><script><style><title>

What you could do is the following, in the Title component:您可以在Title组件中执行以下操作:

export const Tile: React.FC<{ name: string, onHover: SomeType }> = ({ name }) => {
    return (
        <div className="tile-wrapper" onMouseOver={onHover}>
            <h1 className="tile-header>
                {name}
            </h1>
        </div>
    )
}

What you want is to combine React.DOMAttributes<HTMLElement> with your custom props using an intersection.您想要的是使用交集将React.DOMAttributes<HTMLElement>与您的自定义道具结合起来。 That will avail all the DOM events to your custom component, without having to manually pass it in.这会将所有 DOM 事件都用于您的自定义组件,而无需手动将其传入。

export const Tile: React.FC<{ name: string } & React.DOMAttributes<HTMLElement>> = ({ name, ...events }) => {
    return (
        <div className="tile-wrapper" {...events}>
            <h1 className="tile-header>
                {name}
            </h1>
        </div>
    )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在React + Typescript中将ref传递给子级 - Passing ref down to child in React + Typescript React TypeScript将函数传递给子组件 - React TypeScript Passing a function to child component 与 TypeScript 反应:将数字变量传递给子组件 - React with TypeScript: passing a number variable to a child component TypeScript 在反应中将道具传递给子组件时出错 - TypeScript error in passing props to child component in react React 和 typescript,绑定元素 'todos' 隐式具有 'any' 类型。”同时将 props 传递给子组件? - React and typescript, Binding element 'todos' implicitly has an 'any' type." while passing props to child component? 如何将 html 属性传递给孩子<img>元素使用 React 和 Typescript 没有“IntrinsicAttributes &amp; HTMLAttributes<htmlimageelement> 错误”?</htmlimageelement> - How to pass html attributes to child <img> element using React and Typescript without "IntrinsicAttributes & HTMLAttributes<HTMLImageElement> error"? React 使用 TypeScript 和 React Router 将道具传递给子组件 - React passing props to child component using TypeScript and React Router 反应父母对孩子道具传递而不重新渲染 - React parent to child props passing without rerender 将更新的状态值从父级传递给子级。 反应打字稿 - Passing an updated state value from Parent to Child. React Typescript React,使用 TypeScript 将状态设置器(useState)传递给子组件 - React, passing state setter (useState) to child component with TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM