React(七)独立组件间的共享Mixins

本文详细介绍了如何在React项目中使用ES6语法及React Mixin,通过具体步骤展示了下载依赖、创建并使用自定义Mixin的过程,适用于希望增强组件功能的开发者。

(1)ES6的使用 (https://github.com/brigand/react-mixin

  下载依赖包

npm i react-mixin --save

 

(2)导入react-mixin包

import ReactMixin from 'react-mixin'

 

(3)创建js文件 MixinLog.js

const MixinLog = {
  log(){
    console.log('111')
  }
};

export default MixinLog;

 

(4)使用MixinLog

import React, { Component } from 'react';
import logo from './logo.svg';
import
'./App.css'; import ReactMixin from 'react-mixin'; import MixinLog from './components/MixinLog'; class App extends Component { constructor(props) { super(props); this.state = {date: new Date()}; }
  handle() {
    Mixin.log();
  }
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p>{this.state.date.toLocaleTimeString()}</p>
      </div>
    );
  }
}

ReactMixin(App.prototype, MixinLog)

 

转载于:https://www.cnblogs.com/yulingjia/p/9722623.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值