4

我想制作一个可拖动的 div,所以我创建了一个 ref 来访问该 div,这样我就可以在 Rxjs fromEvent 运算符中使用它

问题是 ref 对象将在 fromEvent 中使用后设置(第一次 ref 尚未设置)所以我把它放在 if 语句中,所以它只在设置时使用 ref。

现在 dragRef.current 已设置,但组件不再执行

这是我正在使用的代码:

import React, { useRef, useEffect, useState } from "react";
import "./App.css";
import { fromEvent } from "rxjs";
import { map } from "rxjs/operators";

const App: React.FC = () => {
    const dragRef = useRef<HTMLDivElement>(null);

    const mouseMove = fromEvent(document, "mousedown");

    if (dragRef.current != undefined) {
        debugger;
        const mouseDown = fromEvent(dragRef.current, "mousedown");
        const mouseUp = fromEvent(dragRef.current, "mouseup");
        const mouseDrag = mouseDown.pipe(
            map((evt: MouseEvent | Event) => {
                // let offsetX = evt.clientX - dragRef.current.offsetLeft;
                // let offsetY = evt.clientY - dragRef.current.offsetTop;
                console.log(evt);
                return evt;
            })
        );
        mouseDrag.subscribe(v => console.log(v));
    }

    return (
        <div className="App">
            <header className="App-header">
                <div className="drag" ref={dragRef}></div>
            </header>
        </div>
    );
};

export default App;
4

2 回答 2

1

您可以使用useEffect 挂钩,它将您想要监视的变量数组作为依赖项。因此,在您的情况下,当您的 ref 更改时,您将执行该函数。就像是

useEffect(() => {
if (dragRef.current != undefined) {
        debugger;
        const mouseDown = fromEvent(dragRef.current, "mousedown");
        const mouseUp = fromEvent(dragRef.current, "mouseup");
        const mouseDrag = mouseDown.pipe(
            map((evt: MouseEvent | Event) => {
                // let offsetX = evt.clientX - dragRef.current.offsetLeft;
                // let offsetY = evt.clientY - dragRef.current.offsetTop;
                console.log(evt);
                return evt;
            })
        );
        mouseDrag.subscribe(v => console.log(v));
    }
}, [dragRef])
于 2020-01-24T08:16:08.333 回答
1

这是最终代码,它非常适合 React 中的可拖动 div

import React, { useRef, useEffect, useState } from "react";
import "./App.css";
import { fromEvent } from "rxjs";
import { map, takeUntil, mergeMap } from "rxjs/operators";

const App: React.FC = () => {
  const dragRef = useRef<HTMLDivElement>(null);
  const [position, setPosition] = useState({top: 100, left: 100})

    useEffect(() => {
        if (dragRef != null) {
            const mouseMove = fromEvent(document, "mousemove");
            //@ts-ignore
            const mouseDown = fromEvent(dragRef.current, "mousedown");
            //@ts-ignore
            const mouseUp = fromEvent(dragRef.current, "mouseup");
            const mouseDrag = mouseDown.pipe(
                mergeMap((evt: any) => {
                    //@ts-ignore
                    let offsetX = evt.clientX - dragRef!.current.offsetLeft;
                    //@ts-ignore
                    let offsetY = evt.clientY - dragRef!.current.offsetTop;
                    return mouseMove.pipe(
            map((e: any) => ({
              top: e.clientY - offsetY,
                            left: e.clientX - offsetX
                        })),
            takeUntil(mouseUp),
                    );
                })
      );

            mouseDrag.subscribe((value: any) => {
        console.log(value)
                setPosition(value)
            });
        }
    }, [dragRef]);

    return (
        <div className="App">
            <header className="App-header">
                <div className="drag" ref={dragRef} style={{top: position.top +'px', left: position.left +'px'}}></div>
            </header>
        </div>
    );
};

export default App;

于 2020-01-24T10:02:06.550 回答