简体   繁体   English

React 和 Electron,链接事件处理程序

[英]React and Electron, Chaining event handlers

Is there a way to chain event handlers in React/JavasSript to create more functionality?有没有办法在 React/JavasSript 中链接事件处理程序以创建更多功能?

In the code below, in the div class=resizer I am in need of an event that allows for 'mouse down click', then 'mouse drag' before the function is executed.在下面的代码中,在 div class=resizer中,我需要一个允许“鼠标向下单击”的事件,然后在执行 function 之前进行“鼠标拖动”。

const remote = window.require('electron').remote

const TitlebarDev = () => {

    const window = remote.getCurrentWindow()

    // allows top side resizer with click to restore
    const resizerRestore = () => {
        window.restore()
    }

    const resizerMoveRestore = () => {
        window.restore()
    }

    return (
        <div className="TitlebarDev">
            <div className="Title-BarDev">
                <div className="TitlebarDev-drag-region"></div>
                <div className="Title-BarDev__section-icon">
                </div>
                <div className="Title-BarDev__section-menubar">
                    <div><p>hi</p></div>
                </div>
                <div className="Title-BarDev__section-title">
                    <div className="section-title__title"><p>Node Debug</p></div>
                </div>
                <div className="Title-BarDev__section-windows-control">
                </div>
                <div
                    onDoubleClick={resizerRestore}
                    // needs mouse click down then drag event
                    onMouseDown={resizerMoveRestore}
                    className="resizer">
                </div>
            </div>
        </div >
    )
}

export default TitlebarDev


As I mentioned in the comment, you need to keep the state of your first condition and check it upon the second event taking place.正如我在评论中提到的,您需要保留第一个条件的 state 并在第二个事件发生时检查它。

In the following basic Vanilla JS example, I allowed the movement of the foo element only after it's clicked (and only while the mouse is still down).在下面的基本 Vanilla JS 示例中,我只允许foo元素在单击移动(并且仅在鼠标仍然按下时)。
For this state check I've used the isClicked variable.对于这个 state 检查我使用了isClicked变量。

 const element = document.getElementById('foo'); var isClicked = false; element.addEventListener('mousedown', function() { isClicked = true; }); element.addEventListener('mouseup', function() { isClicked = false; }); document.addEventListener("mousemove", function(event) { if (isClicked) { element.style.left = (event.clientX - 25) + 'px'; element.style.top = (event.clientY - 25) + 'px'; // Please note that I arbitrarily chosen foo's // width and height to be 50px, hence the 25px offset } });
 #foo { width: 50px; height: 50px; background-color: tomato; position: absolute; }
 <div id="foo"></div>

If you're using React, you can always (and preferably) use your state to store these... states;)如果你使用 React,你总是可以(并且最好)使用你的state来存储这些......状态;)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM