前话

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. C# static的用法详解

    C#   static的用法详解 有的东西你天天在用,但未必就代表你真正了解它,正如我之前所了解的 static . 一.静态类 静态类与非静态类的重要区别在于静态类不能实例化,也就是说,不能使用 n ...

  2. showDoc的基本使用方法

    ShowDoc介绍 ShowDoc就是一个非常适合IT团队的在线文档分享工具,它可以加快团队之间沟通的效率. API文档( 查看Demo) 随着移动互联网的发展,BaaS(后端即服务)越来越流行.服务 ...

  3. P1136 迎接仪式

    P1136 迎接仪式 $O(n^{2}k)$:$f[i][k]$表示到第$i$个字符为止,交换$k$次,得到的最多子串数 那么枚举位置$j$,状态可以从$f[j][k-1]+1$转移过来 $O(nk^ ...

  4. 20145303刘俊谦《网络对抗》Exp2 后门原理与实践

    20145303刘俊谦<网络对抗>Exp2 后门原理与实践 基础问题回答 1.例举你能想到的一个后门进入到你系统中的可能方式? •在网页上浏览不安全的网站或者下载不安全的软件 •通过发送邮 ...

  5. Vim 的光标移动定位

    一.光标移动以单个字符为单位: 在命令模式中 h向左 l 向右 j 向上 k 向下 二.光标移动以word 为单位: w 将光标向前移动一个word; b 将光标向后移动一个word: 以上2个命令光 ...

  6. 使用SpringBoot发送邮件

    最后发送成功后,感觉SpringBoot真的很强大. http://www.ykmimi.com/email ↑待加入email输入的重载(可以不上传文件或可以不填写主内容) ↑待加入邮箱RegExp ...

  7. codevs 1690 开关灯 线段树+延迟标记

    1690 开关灯  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这 ...

  8. VuePress从零开始搭建自己的博客

    VuePress是什么? VuePress是以Vue驱动的静态网站生成器,是一个由Vue.Vue Router和webpack驱动的单页应用.在VuePress中,你可以使用Markdown编写文档, ...

  9. SpringBoot在启动时的多环境配置以及加载顺序

    通常我们在开发完成一个SpringBoot项目时,总是要打包部署的. 在启动SpringBoot应用时,我们常常会使用命令java -jar xxx.jar来启动这个服务. 命令java -jar 除 ...

  10. SQLPLUS的乱码问题

    我的中文系统,把对应非unicode字符时的设置,改成了 日文, 结果控制台使用sqlplus时候,总是出现乱码. 解决方法是,把NLS_LANG环境变量变成跟系统一样,就可以了. american_ ...