16、react之 MIxin

来源:互联网 发布:cydia添加源网络错误 编辑:程序博客网 时间:2024/05/22 01:40


import React from 'react';var BindingMixin = {    handleChange:function(key){        var This = this;        return function(e){            var newState = {};            newState[key] = e.target.value;            This.setState(newState)        }    }}class Binging extends React.Component {    constructor(props){        //super(props)才能使用this        super(props);        this.state={            text:'',            comment:''        }        this.handleChange = BindingMixin.handleChange    }    render(){        return (            <div>                <input type="text" onChange={this.handleChange('text')}/>                <p>{this.state.text}</p>                <textarea onChange={this.handleChange('comment')}></textarea>                <p>{this.state.comment}</p>            </div>        )    }}export default Binging;