在create-react-app创建的项目下允许函数绑定运算符
前话
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} />
}
}
- stage 0 的 transform-function-bind
export default class CartItem extends React.Component {
method(){...};
render() {
<input type="button" value="点我" onClick={::this.method} />
}
}
最后一种很帅气, 然并软,我使用就直接报错。 臣不服,不服。
于是寻找方案, 因为是create-react-app创建的项目。
我的思考方案如下
- create-react-app内置支持的配置
在awesome-create-react-app中找到how-to-use-custom-babel-presets, 看到了光芒,光芒啊。
高兴太早,死的也早,Adding support for custom babel configuration被拒绝了,大致是,想法非常好,非常好,就是不能。那么,我走下一条路 - npm run eject
采用eject后,会多出很多文件,并且是不可逆向的。
恶心,恶心,那么多文件。我要出去透透气。 - react-app-rewired
这个比较简单一些。
寻寻觅觅找到了injectbabelplugin
然后找到对应的插件babel-preset-stage-0,babel-plugin-transform-function-bind
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创建的项目下允许函数绑定运算符的更多相关文章
- Create React App
Facebook开源了React前端框架(MIT Licence),也同时提供了React脚手架 - create-react-app. create-react-app遵循约定优于配置(Coc)的原 ...
- 如何扩展 Create React App 的 Webpack 配置
如何扩展 Create React App 的 Webpack 配置 原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...
- 深入 Create React App 核心概念
本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...
- 在 .NET Core 5 中集成 Create React app
翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] Camilo Reyes ...
- tap news:week5 0.0 create react app
参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...
- 使用create react app教程
This project was bootstrapped with Create React App. Below you will find some information on how to ...
- Create React App 安装less 报错
执行npm run eject 暴露模块 安装 npm i less less-loader -D 1.打开 react app 的 webpack.config.js const sassRege ...
- create react app 项目部署在Spring(Tomcat)项目中
网上看了许多,大多数都是nginx做成静态项目,但是这样局限性太多,与Web项目相比许多服务端想做的验证都很麻烦,于是开始了艰难的探索之路,终于在不经意间试出来了,一把辛酸... 正常的打包就不说了. ...
- [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 ...
随机推荐
- Thread的六中状态
线程共有6种状态:在某一时刻只能是这6种状态之一.这些状态由Thread.State这个枚举类型表示,并且可以通过getState()方法获得当前线程具体的状态类型. NEW:至今尚未启动的线程的状态 ...
- HDU5183 hash 表
做题的时候忘了 数据结构老师说的hash表了, 用二分找,还好过了, hash 表 对这题 更快一些 #include <iostream> #include <algorithm& ...
- 对java沙箱机制的一点了解
1. 引入 我们都知道,程序员编写一个Java程序,默认的情况下可以访问该机器的任意资源,比如读取,删除一些文件或者网络操作等.当你把程序部署到正式的服务器上,系统管理员要为服务器的安全承担责任, ...
- 20145325张梓靖 《Java程序设计》第3周学习总结
20145325张梓靖 <Java程序设计>第3周学习总结 教材学习内容总结 类与对象 类,就相当于设计图纸,用"new"创建的对象,就是依据设计图做成的成品:设计图纸 ...
- python常见容器属性和方法
`````字符串中反斜杠字符表 转义格式 意义 \' 单引号(') \" 双引号(") \\ 反斜杠(\ ) \n 换行 \r 返回光标至行首 \f 换页 \t ...
- Access规格
属性 最大值 Microsoft Access 数据库 (.mdb) 文件大小 2G 字节减去系统对象所需的空间. 数据库中的对象个数 32,768 模块(包括“内含模块”属性为“是”的窗体和报表) ...
- numpy数组广播
numpy数组的广播功能强大,但是也同时让人疑惑不解,现在让我们来谈谈其中的原理. 广播原则: 如果两个数组的后缘维度(即:从末尾开始算起的维度)的轴长相符或其中一方的长度为1,则认为它们是广播兼容的 ...
- 51nod 1289 大鱼吃小鱼
#include<bits/stdc++.h> using namespace std; ; int a[maxn],b[maxn]; stack<int>s; int mai ...
- shell 判断字符串长度是否不为0
test.sh #!/bin/bash s1="" if test $s1 ;then echo "length is not zero" else echo ...
- 在Angular中定义共享的Providers
转自:https://segmentfault.com/a/1190000010700308 有时,你需要在 Angular 应用中创建一个共享模块,该模块定义了功能模块和lazy-loaded模块可 ...