使用Mixins实现不同组件之间的共享功能,共享代码
ES6语法下使用Mixins需要安装插件react-mixin
npm install react-mixin --save
应用举例:
mixins.js文件
const MixinLog = {
log(){
console.log("我是xiaohujiazi");
}
}
export default MixinLog;
bosyindex.js(引入mixins.js的文件)
import ReactMixin from 'react-mixin';
import MixinLog from './mixins';
constructor() {
super();//调用基类的所有的初始化的方法
this.state = {username: "xiaoming", age: 20};
}
changeUserInfo(age) {
MixinLog.log();
}
render(){
return (
<div>
<input id="submitButton" type="button" value="提交" onClick={this.changeUserInfo.bind(this,99)}/>
</div>
)
}
}
ReactMixin(BodyIndex.prototype, MixinLog);