Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rwson/bind-with-arguments

easily bind events excute context and dynamic arguments in React
https://github.com/rwson/bind-with-arguments

arguments async-await autobind callback click context react

Last synced: about 1 month ago
JSON representation

easily bind events excute context and dynamic arguments in React

Awesome Lists containing this project

README

        

## bind-with-arguments

彻底解决`React`事件绑定中`this`和参数的问题

### usage

```powershell
npm install bind-with-arguments --save
```

```jsx
import React, { Component } from 'react';
import bindWithArguments from 'bind-with-arguments';

class Comp extends Component {

async loadDdetail(id) {
// fetch
}

// 异步函数并且有参数
@bindWithArguments
async asyncParams(id) {
const detail = await this.loadDdetail(id);
// ...
}

// 异步函数并且无参数
@bindWithArguments
async asyncNoParams() {
// fetch
this.setState({
xxx: 'xxx'
});
}

// 同步函数并且有参数
@bindWithArguments
syncParams(id, name) {
if (id && name) {
this.setState({
xxx: 'xxxx'
});
}
}

// 同步函数并且有参数
@bindWithArguments
syncNoParams() {
const { yyy } = this.state;
if (yyy === 'zzz') {
this.setState({
xxx: 'xxxx'
});
}
}


render() {
const { list } = this.state;

return (


{
list.map((row) => {
return (

异步函数并且有参数
同步函数并且有参数

);
})
}
异步函数并且无参数
同步函数并且有参数

)
}

}
```