zoukankan      html  css  js  c++  java
  • react 中的绑定事件

      handleOpen = (e)=> {
        this.setState({
          open: true
        })
      }
      <Button color='primary' onClick={this.handleOpen}>打开模态框</Button>
    

    在构造函数里面 bing

        constructor(props){
          super(props);
          this.handleOpen = this.handleOpen.bind(this);
        }
    
      handleOpen(e) {
        this.setState({
          open: true
        })
      }
      
    <Button color='primary' onClick={this.handleOpen}>打开模态框</Button>
    

    获取点击事件的元素

    	asd = e=>{
    		console.log(e.currentTarget); // div>span
    		console.log(e.target); // span
    	}
    
    		return ( 
    			<React.Fragment> 
    				<div onClick={this.asd}><span>click me</span></div>
    			</React.Fragment>
    		);
    

    传递参数

      handleOpen = hello => ({target}) =>{
        l(hello,  target)
      }
    
      <Button color='primary' onClick={this.handleOpen('hello')}>打开模态框</Button>
    

    e.preventDefault(); 阻止默认行为
    e.stopPropagation() 阻止事件传播(冒泡)
    支持的事件名

    rxjs 防抖

    <RootRef rootRef={e => this.button = e}>
          <Button color='secondary'>Get Json</Button>
    </RootRef>
    
      componentDidMount() {
        fromEvent(this.button, 'click')
          .pipe(
        throttleTime(2000))
          .subscribe(async v => {
            let res = await ajax('http://localhost:1995/a')
              .pipe(map(res => res.response))
              .toPromise();
            store
              .state
              .users
              .push(res)
          })
      }
    

    lodash 防抖

    // 带参数
      <Button color='secondary' onClick={this.handleClick('ajanuw')}>Get Json</Button>
      handleClick = (name) => throttle((e) => {
        l(name)
      }, 2000)
    
    // 不带参数
      <Button color='secondary' onClick={this.handleClick}>Get Json</Button>
      handleClick = throttle((e) => {
        l(1)
      }, 2000)
    

    rxjs debounce

    class Hello extends Component {
      list = new Array(20).fill(0);
    
      render() {
        return (
          <div
            ref={this.props.helloRef}
            style={{
               400,
              height: 200,
              border: "1px solid red",
              overflow: "auto",
            }}
          >
            {this.list.map((el, i) => (
              <li key={i}>{i}</li>
            ))}
          </div>
        );
      }
    }
    
    class Test extends Component {
      helloRef = React.createRef();
      componentDidMount() {
        fromEvent(this.helloRef.current, "scroll")
          .pipe(debounceTime(20))
          .subscribe(v => {
            l(v.target.scrollTop);
          });
      }
      render() {
        return <Hello helloRef={this.helloRef} />;
      }
    }
    
  • 相关阅读:
    AcWing 524. 愤怒的小鸟
    AcWing 算法提高课题解目录
    AcWing 292. 炮兵阵地
    AcWing 798. 差分矩阵
    golang 写数据到excel文件 清明
    使用golang开发mqtt服务压力测试工具 清明
    Linux云服务器安装JDK步骤 清明
    shell monitor memory 清明
    自己实现一个Electron跨进程消息组件(兼新书自荐)
    如何把Electron做成一个Runtime,让多个应用共享同一个Electron
  • 原文地址:https://www.cnblogs.com/ajanuw/p/9416819.html
Copyright © 2011-2022 走看看