React

React(组件的this绑定)

2017-10-26  本文已影响8人  余生筑
class Epp extends Component {
    constructor()
    {
        super();
        this.fn=this.fn.bind(this);//注意这一步需在constructor()中设置    
    }
    fn(event)
    {
        console.log('123') 
    }

    render() {
        return (
            <div onClick={this.fn}>
            123
            </div>  
            );
        } 
    }
class Epp extends Component {
    constructor()
    {
        super();
    }
    fn(event)
    {
        console.log('123') 
    }

    render() {
        return (
            <div onClick={this.fn.bind(this)}>
            123
            </div>  
            );
        } 
    }
class Epp extends Component {
    constructor()
    {
        super();
        this.fn=this.fn.bind(this);     
    }
    fn=(event)=>
    {
        console.log('123') 
    }

    render() {
        return (
            <div onClick={this.fn}>
            123
            </div>  
            );
        } 
    }
上一篇下一篇

猜你喜欢

热点阅读