Giter Site home page Giter Site logo

delegate-to's Introduction

version Build Status Coverage

delegate-to

DOM Event delegate, it works great in React, too

Clever API

Zero dependence

React Server-side rendering compatible

All browsers supported

Compare with others

zenorocha/delegate is a popular library for delegate event:

delegate(document.body, '.btn', 'click', function(e) {
  console.log(e.delegateTarget)
}, false)

Works in React.js

render () {
  <div>
    <div ref="foo" />
  </div>
}

componentDidMount() {
  delegate(this.refs.foo, '.btn', 'click', this.handleClick, false)
}

Switch to delegate-to

document.body.addEventListener('click', delegate('.btn', e => {
  console.log(e.delegateTarget)
}), false)

Then in React.js

render () {
  <div>
    <div onClick={delegate('.btn', this.handelClick)} />
  </div>
}

You can see the advantage in delegate-to

  • Fewer API interface (only 2 arguments)

  • Works with normal addEventListener

  • React(.jsx) friendly

  • Flexibly, custom match condition

Installation

$ npm i --save delegate-to

Examples

import delegate from 'delegate-to'
  
render () {
  <div>
    <div onClick={delegate('.btn', this.handelClick)} />
  </div>
}
    
// custom match condition
render () {
  <div>
    <div onClick={delegate(target => target.classList.contains('btn'), this.handelClick)} />
  </div>
}

API

delegate([selector || condition func], [dispatchEvent])

Browsers Supported

delegate-to use native el.matchs DOM API to match CSS selector, it works on IE 9+ and All morden browsers, you can try custom match condition function if you need to work with old browsers.

delegate-to's People

Contributors

rwu823 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.