Recommended way of making React component/div draggable

后端 未结 9 686
耶瑟儿~
耶瑟儿~ 2020-11-28 18:13

I want to make a draggable (that is, repositionable by mouse) React component, which seems to necessarily involve global state and scattered event handlers. I can do it the

9条回答
  •  离开以前
    2020-11-28 18:32

    I would like to add a 3rd Scenario

    The moving position is not saved in any way. Think of it as a mouse movement - your cursor is not a React-component, right?

    All you do, is to add a prop like "draggable" to your component and a stream of the dragging events that will manipulate the dom.

    setXandY: function(event) {
        // DOM Manipulation of x and y on your node
    },
    
    componentDidMount: function() {
        if(this.props.draggable) {
            var node = this.getDOMNode();
            dragStream(node).onValue(this.setXandY);  //baconjs stream
        };
    },
    

    In this case, a DOM manipulation is an elegant thing (I never thought I'd say this)

提交回复
热议问题