Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

javascript - How change onMouseMove handler in other handler with react component?

How we can change the handlerOnMouseMove inside other handler (in my example OnClick). I show an example below; normally when I do this this.handleMouseMove = undefined; it should disable my event onMouseMove but unfortunately it is not working.

import React from 'react' 
import {render} from 'react-dom'
import './BasicComponent.css'

class BasicComponent extends React.Component {

  constructor (props){
    super(props)
    this.state = {
      id: "id",
      title: "component",
      inputs: [],
      outputs: [],
    }

    this.handleClick = this.handleClick.bind(this)
    this.handleMouseDown = this.handleMouseDown.bind(this)
    this.handleMouseUp = this.handleMouseUp.bind(this)
    this.handleMouseMove = this.handleMouseMove.bind(this)

  }
    render() {
        console.log("render");
        return(
          <div  className="component" 
                onMouseDown={ this.handleMouseDown } 
                onMouseUp={ this.handleMouseUp }
                onMouseMove={ this.handleMouseMove }>
            <div className="title">Title</div>
            <div className="id">ID: c_356545454</div>
            <div className="inputs">inputs</div>
            <div className="core">core</div>
            <div className="outputs">outputs</div>
            <button onClick={ this.handleClick } >Disable handler onMouseMove</button>
          </div>
        );
    }

    handleClick() {
      this.handleMouseMove = undefined; // <===== this not disable the call on handleMouseMove ??? 
      console.log("handleClick : handleMouseMove is disabled");
    }

    handleMouseDown() {
      console.log("handleMouseDown");
    }

    handleMouseUp() {
      console.log("handleMouseUp");
    }

    handleMouseMove() {
      console.log("handleMouseMove");
    }
}

export default BasicComponent
question from:https://stackoverflow.com/questions/65871597/how-change-onmousemove-handler-in-other-handler-with-react-component

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

class BasicComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      id: "id",
      title: "component",
      inputs: [],
      outputs: [],
      disableMouseMove: false,
    };

    this.handleClick = this.handleClick.bind(this);
    this.handleMouseDown = this.handleMouseDown.bind(this);
    this.handleMouseUp = this.handleMouseUp.bind(this);
    this.handleMouseMove = this.handleMouseMove.bind(this);
  }
  render() {
    const { disableMouseMove } = this.state;
    return (
      <div
        className="component"
        onMouseDown={this.handleMouseDown}
        onMouseUp={this.handleMouseUp}
        onMouseMove={disableMouseMove ? () => {} : this.handleMouseMove}
      >
        <div className="title">Title</div>
        <div className="id">ID: c_356545454</div>
        <div className="inputs">inputs</div>
        <div className="core">core</div>
        <div className="outputs">outputs</div>
        <button onClick={this.handleClick}>Disable handler onMouseMove</button>
      </div>
    );
  }

  handleClick() {
    this.setState({ disableMouseMove: true }); // <===== this not disable the call on handleMouseMove ???
    console.log("handleClick : handleMouseMove is disabled");
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...