前话

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. maven-eclipse 中index.html页面乱码

    maven-eclipse 中index.html页面乱码: pox.xml修改: <project> -- <properties> <argLine>-Dfil ...

  2. oauth2(转载http://www.rollosay.com/it/%E4%BD%BF%E7%94%A8OAuth-Server-PHP%E5%AE%9E%E7%8E%B0OAuth2%E6%9C%8D%E5%8A%A1)

    http://www.rollosay.com/it/%E4%BD%BF%E7%94%A8OAuth-Server-PHP%E5%AE%9E%E7%8E%B0OAuth2%E6%9C%8D%E5%8A ...

  3. 解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题

    mybatis中mapUnderscoreToCamelCase的使用 mybatis-config.xml配置: <?xml version="1.0" encoding= ...

  4. vs显示行号

    1.工具 2.选项 3.文本编辑器 4.C\C++ 5.常规->显示行号

  5. Signing package index... Cannot open file '/home/jello/openwrt/key-build' for reading

    一.环境 发行版:Ubuntu 18.04.1 LTS 代号:bionic 内核版本:4.15.0-30-generic 二.背景 在编译Openwrt/LEDE时出现以下错误,进而自动终止了编译: ...

  6. Gradle sync failed: Read timed out

    : Gradle sync started : Gradle sync failed: Read timed out Consult IDE log m s ms) 原因是Gradle下载超时 一.下 ...

  7. Morgan logger

    var logger = require("morgan"); res.render = function(view, options, fn){ options = option ...

  8. 在x86为arm 编译 httpd 2.2.31

    这个版本的httpd 已经自带 apr apr-util pcre , 不用额外下载源代码 1) 编写环境变量脚本,并执行 cross-env.sh : export ARMROOTFS=/h1roo ...

  9. UVALive-3972 March of the Penguins (最大流:节点容量)

    题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...

  10. IOS UI-标签(Label)的高级应用

    // // BWLabel.h // IOS_0119_label // // Created by ma c on 16/1/19. // Copyright © 2016年 博文科技. All r ...