在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 ...
随机推荐
- maven-eclipse 中index.html页面乱码
maven-eclipse 中index.html页面乱码: pox.xml修改: <project> -- <properties> <argLine>-Dfil ...
- 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 ...
- 解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题
mybatis中mapUnderscoreToCamelCase的使用 mybatis-config.xml配置: <?xml version="1.0" encoding= ...
- vs显示行号
1.工具 2.选项 3.文本编辑器 4.C\C++ 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时出现以下错误,进而自动终止了编译: ...
- Gradle sync failed: Read timed out
: Gradle sync started : Gradle sync failed: Read timed out Consult IDE log m s ms) 原因是Gradle下载超时 一.下 ...
- Morgan logger
var logger = require("morgan"); res.render = function(view, options, fn){ options = option ...
- 在x86为arm 编译 httpd 2.2.31
这个版本的httpd 已经自带 apr apr-util pcre , 不用额外下载源代码 1) 编写环境变量脚本,并执行 cross-env.sh : export ARMROOTFS=/h1roo ...
- UVALive-3972 March of the Penguins (最大流:节点容量)
题目大意:有n个带有裂缝的冰块.已知每个冰块的坐标和已经站在上面的企鹅数目,每当一个企鹅从一个冰块a跳到另一个冰块b上的时候,冰块a上的裂缝便增大一点,还知道每个冰块上最多能被跳跃的次数.所有的企鹅都 ...
- IOS UI-标签(Label)的高级应用
// // BWLabel.h // IOS_0119_label // // Created by ma c on 16/1/19. // Copyright © 2016年 博文科技. All r ...