在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 ...
随机推荐
- Educational Codeforces Round 58 Solution
A. Minimum Integer 签到. #include <bits/stdc++.h> using namespace std; #define ll long long ll l ...
- HTTP--TCP连接
几乎所有的 HTTP 通信都是由 TCP/IP 承载的,TCP/IP 是全球计算机及网络 设备都在使用的一种常用的分组交换网络分层协议集.客户端应用程序可以打开一 条 TCP/IP 连接,连接到可能运 ...
- DigitalOcean(edu用户)搭建VPS
免费上网FQ edu福利 edu邮箱 VPS VPN 1 申请 目的:利用edu邮箱的优惠获得digitalocean一年vpn,可以FQ或者免流. 1.1 准备工作 百度“github大礼包”,浏览 ...
- Tensorflow代码解析(一)
http://www.leiphone.com/news/201702/n0uj58iHaNpW9RJG.html?utm_source=tuicool&utm_medium=referral ...
- Gym 101246H ``North-East''(LIS)
http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的 ...
- 语义化标签和media媒体查询可以放心使用
现在的高级浏览器都支持html5,只有IE6-IE8不支持.(下面说的IE均值IE6-IE8) 有两个特性在IE是可以使用的: 1.语义化标签: header(头部) section(区块) foot ...
- 智课雅思词汇---二十七、形容词后缀-ant/-ent
智课雅思词汇---二十七.形容词后缀-ant/-ent 一.总结 一句话总结: ...的 后缀:-ant ①[形容词后缀] 大部分与-ance或-ancy,相对应,表示属于...的.具有...性质的 ...
- C#使用(NamedPipe)命名管道通信的例子
https://blog.csdn.net/yl2isoft/article/details/20228279
- 2-14-2 MySQL数据类型
MySQL数据类型: 对数据进行分类,针对不同分类进行不同的处理. 1. 使系统能够根据数据类型来操作数据. 2. 预防数据运算时出错. 3. 更有效的利用空间. 数据分类,可以使用最少的存储,来存放 ...
- zoj-3329-期望/dp/方程优化
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...