简体   繁体   English

如何从 React 组件中向 BODY 添加点击处理程序?

[英]How can I add a click handler to BODY from within a React component?

I am building a pulldown menu React component that should close when the user clicks anywhere in the DOM outside of the component.我正在构建一个下拉菜单 React 组件,当用户单击组件外部DOM 中的任何位置时,该组件应该关闭。

Using jQuery I would typically add an event listener to the body when the pulldown is opened, and remove it again when the pulldown is closed.使用 jQuery,我通常会在下拉菜单打开时向body添加一个事件侦听器,并在下拉菜单关闭时再次将其删除。 (The event listener itself closes the pulldown – any click events within the component are not propagated to prevent the body click handler from firing.) (事件侦听器本身关闭下拉菜单——组件的任何点击事件都不会传播,以防止主体点击处理程序被触发。)

Is there any way to attach a listener to the body element from within a React component?有没有办法从 React 组件中将侦听器附加到body元素? Or should I just use jQuery?还是我应该只使用 jQuery? (I'm a bit wary of mixing React and jQuery.) (我有点担心混合 React 和 jQuery。)

React is just JavaScript so attaching a click handler to any element is done as normal by using addEventListener() . React 只是 JavaScript,因此可以通过使用addEventListener()像往常一样将点击处理程序附加到任何元素。 Doing this in componentDidMount is normally very nice and tidy and clean up after yourself in componentWillUnmount by removing the added event handler.componentDidMount执行此操作通常非常好且整洁,并通过删除添加的事件处理程序在componentWillUnmount进行清理。

var Component = React.createClass({
    componentDidMount: function () {
        document.body.addEventListener('click', this.myHandler);
    },
    componentWillUnmount: function () {
        document.body.removeEventListener('click', this.myHandler);
    },
    myHandler: function () {
        alert('click');
    },
    render: function() {
        return <div>Hello {this.props.name}</div>;
    }
});

It's help who used function based components:使用基于函数的组件是有帮助的:

import React, { useEffect  } from 'react';

const ZSideBar = (props) => {

    useEffect(() => {
        document.body.addEventListener('click', closeSidemenu );

        return function cleanup() {
            window.removeEventListener('click', closeSidemenu );
        } 
    },[]);
    let closeSidemenu = () => {
        document.getElementById("sidebar-tree").style.left = "-310px";
    }
   
    return (
        <div></div>
    )

}

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

相关问题 如何在反应组件中监听身体点击事件 - How to listen for body click event within a react component 当我单击该行中的React组件链接时,如何防止该行单击? - How can I prevent a row click when I click a React component link within that row? 如何通过引用组件添加Click Handler - How to add Click Handler through reference of component 添加事件处理程序以获取 Button 单击 LeadPage,我可以在其中对正文进行编码 - Add event handler to get Button Click on a LeadPage where I can just code on the body 在 React 中,我可以在另一个功能组件的主体中定义一个功能组件吗? - In React, can I define a functional component within the body of another functional component? 在 React 中,如何从不同组件的事件处理程序触发自定义按钮的单击事件? - In React how can I trigger a custom button's click event from a different compoennt's event handler? 我可以在 React Functional 组件中添加异步函数吗 - Can i add Asynchronous function within React Functional Component 如何添加点击处理程序功能以在Vue中动态链接? - How i can add click handler function to dynamically link in Vue? 如何在React中将对象附加到组件主体? - How can I append objects to the body of a component in React? 如何添加事件以响应组件? - How can I add an event to react component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM