美文网首页ReactiveNative
React native-事件绑定

React native-事件绑定

作者: FlyElephant | 来源:发表于2019-01-26 09:05 被阅读7次

React 使用ES6+语言时候不像之前的非ES6一样进行自动绑定,需要手动通过.bind(this)或者使用箭头函数=>.实际写法有三种.

直接绑定

    _onChange(event) {
        console.log(event);
    }

    render() {
        return <FlyElephantView onChange={this._onChange.bind(this)} />
    }

预绑定

初始化的时候绑定this,进行事件处理.

    constructor(...args) {
        super(...args);
        this._onChange = this._onChange.bind(this);
    }
    _onChange(event) {
        console.log(event);
    }

    render() {
        return <FlyElephantView onChange={this._onChange} />
    }

箭头函数

箭头函数绑定比较简单,实现如下:

    _onChange = (event) => {
        console.log(event);
    }

    render() {
        return <FlyElephantView onChange={this._onChange} />
    }
}

相关文章

网友评论

    本文标题:React native-事件绑定

    本文链接:https://www.haomeiwen.com/subject/vtuovxtx.html