前话

React的函数绑定一致是个问题,主要有下面几种方式:

  • 事件处理器动态绑定
export default class Com extends React.Component {
render() {
<input type="button" value="点我" onClick={this.method.bind(this)} />
}
}
  • 构造函数绑定
export default class CartItem extends React.Component {

    constructor(props) {
super(props);
this.method = this.method.bind(this);
} render() {
<input type="button" value="点我" onClick={this.method} />
}
}
  • 构造函数 + 箭头函数 | 箭头函数
export default class CartItem extends React.Component {

    const method2 = () =>{...}

    constructor(props) {
super(props);
this.method = (ev) => {...}
} render() {
<input type="button" value="点我" onClick={this.method} />
}
}

这个是babel支持的,还不是标准

export default class CartItem extends React.Component {

    method = () => {...};

    render() {
<input type="button" value="点我" onClick={this.method} />
}
}

export default class CartItem extends React.Component { method(){...}; render() {
<input type="button" value="点我" onClick={::this.method} />
}
}

最后一种很帅气, 然并软,我使用就直接报错。 臣不服,不服。

于是寻找方案, 因为是create-react-app创建的项目。

我的思考方案如下

const rewireMobX = require('react-app-rewire-mobx');
const rewireEslint = require('react-app-rewire-eslint');
const {injectBabelPlugin} = require('react-app-rewired'); /* config-overrides.js */
module.exports = {
webpack: function override(config, env) {
config = rewireEslint(config, env);
config = rewireMobX(config, env);
config = injectBabelPlugin('transform-function-bind',config) config.output.publicPath = ''
return config;
}
}

修改完毕,启动,哦,可以。 真是不错。

扔掉键盘,甩开鼠标,深深的一口水,想写行代码咋这么难。

React and ES6 - Part 3, Binding to methods of React class (ES7 included)

decorator

create-react-app

how-to-use-custom-babel-presets

Adding support for custom babel configuration #1357

react-app-rewired

injectbabelplugin

babel-preset-stage-0

babel-plugin-transform-function-bind

在create-react-app创建的项目下允许函数绑定运算符的更多相关文章

  1. Create React App

    Facebook开源了React前端框架(MIT Licence),也同时提供了React脚手架 - create-react-app. create-react-app遵循约定优于配置(Coc)的原 ...

  2. 如何扩展 Create React App 的 Webpack 配置

    如何扩展 Create React App 的 Webpack 配置  原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...

  3. 深入 Create React App 核心概念

    本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...

  4. 在 .NET Core 5 中集成 Create React app

    翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] Camilo Reyes ...

  5. tap news:week5 0.0 create react app

    参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...

  6. 使用create react app教程

    This project was bootstrapped with Create React App. Below you will find some information on how to ...

  7. Create React App 安装less 报错

    执行npm run eject 暴露模块 安装 npm i  less less-loader -D 1.打开 react app 的 webpack.config.js const sassRege ...

  8. create react app 项目部署在Spring(Tomcat)项目中

    网上看了许多,大多数都是nginx做成静态项目,但是这样局限性太多,与Web项目相比许多服务端想做的验证都很麻烦,于是开始了艰难的探索之路,终于在不经意间试出来了,一把辛酸... 正常的打包就不说了. ...

  9. [React] Use the Fragment Short Syntax in Create React App 2.0

    create-react-app version 2.0 added a lot of new features. One of the new features is upgrading to Ba ...

随机推荐

  1. Python实现支付宝在线支付

    windows系统安装Python虚拟环境 首先保证你的系统已经安装好了Python 安装virtualenv C:\WINDOWS\system32>pip3 install virtuale ...

  2. 计算机bit是什么意思

    bit是计算机中数据的最小单位,即二进制位,数字0和1 一个字节是八位(8个0和1 或 1 组成的一串二进制) 一个字是16位,等于2个字节 用八位二进制表示的字符叫单字节字符, 用16位二进制数表示 ...

  3. 【知识总结】Java类初始化顺序说明

    微信公众号:努力编程的小猪如有问题或建议,请公众号留言 Java类初始化顺序说明 一个类中包含如下几类东西,他们前后是有顺序关系的 静态属性:static 开头定义的属性 静态方法块: static ...

  4. SQL学习笔记八之ORM框架SQLAlchemy

    阅读目录 一 介绍 二 创建表 三 增删改查 四 其他查询相关 五 正查.反查 一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进 ...

  5. windows系统中 利用kibana创建elasticsearch索引等操作

    elasticsearch之借用kibana平台创建索引 1.安装好kibana平台 确保kibana以及elasticsearch正常运行 2.打开kibana平台在Dev Tools 3.创建一个 ...

  6. ExtJS错误解决 Cannot read property 'on' of undefined

    背景 用ExtJS新写了一个功能,运行时控制台打印错误Cannot read property 'on' of undefined,出错代码位置是Ext.define.bindStoreListene ...

  7. MVC webapi,Action的分别拦截器

    继承Controller,ApiController的拦截继承的接口不一样 webapi的拦截器:(继承System.Web.Http) using System; using System.Coll ...

  8. DataContract with Json.Net

    https://www.newtonsoft.com/json/help/html/DataContractAndDataMember.htm 如果class使用了DataContract,name没 ...

  9. Asynchronous Programming Patterns

    Asynchronous Programming Patterns The .NET Framework provides three patterns for performing asynchro ...

  10. 【建项目】eclipse maven建立多模块工程

    在工作的时候,大多时候都是用Maven来管理项目,可是一般我们都知道怎么用maven管理工程,却不知道通过Maven自己来建立多模块工程.于是自己抽时间,在网上找些资料,做了起来. 建立简单的Mave ...