在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 ...
随机推荐
- cocos代码研究(23)Widget子类ScrollView学习笔记
基础理论 一个能够被用户触摸滚动的一个层次型布局容器视图,允许其尺寸大于屏幕显示的物理尺寸,其内部维护有一个布局用于水平的或垂直的存放子节点.继承自 Layout,被 ListView 继承. 代码实 ...
- 39XML文档类
Xml源代码 domxml.h #ifndef DOMXML_H #define DOMXML_H #include <QString> #include <QStringList& ...
- 史上最强网推案例,没有之一【ZW团队实战经典】
ZW团队认为,互联网本质只有两个字:颠覆. ZW网络推广团队,是国内首个教父级网络营销团队,自1997年以来,先后参与操盘多个重大互联网项目,服务过超过150家国际500强客户,是微软公司首家官方认证 ...
- 20145309李昊《网络对抗》MSF应用基础
实验内容 掌握metasploit的基本应用方式1.主动攻击——ms08_0672.针对浏览器的攻击——ms11_0503.针对客户端的攻击——Adobe4.成功应用一个辅助模块——scanner/d ...
- JQuery+CSS3实现Ajax加载时loading效果
之前通过Ajax请求加载数据的时候,在数据还没有呈现出来前,为了更好的用户体验,总会弄个loading告诉用户其实内容正在加载,而不是网站崩了.但是貌似之前使用gif图片的情况比较多,可能是为了兼容各 ...
- InstallShieldPro 2015中Basic MSI工程类型如何调用InstallScript脚本
转载:http://blog.csdn.net/mengzhishen/article/details/8953901 这是一个Basic MSI基础知识. A. 首先在Installation De ...
- Qt+json
Json文件是这样: { "first fruit": { "describe":"an apple", "icon": ...
- 更改idea快捷键方式为eclipse风格
打开配置窗口 菜单栏中的File-settings 或者快捷键 ctrl+alt+s 设置keymap 在弹出的setting页面中左侧导航中选择Keymap: 在keymaps下拉列表中选择Ecli ...
- UVa 10534 波浪子序列(快速求LIS)
https://vjudge.net/problem/UVA-10534 题意:给定一个长度为n的整数序列,求一个最长子序列(不一定连续),使得该序列的长度为2k+1,前k+1个数严格递增,后k+1个 ...
- POJ 3259 Wormholes(Bellman-Ford)
http://poj.org/problem?id=3259 题意:有一些普通的洞和虫洞,每个洞都有经过的时间,虫洞的时间是负的,也就是时光倒流,问是否能回到出发时的时间. 思路: 贝尔曼-福特算法判 ...