
在create-react-app创建的项目下允许函数绑定运算符
发布日期:2021-05-09 01:29:49
浏览次数:12
分类:博客文章
本文共 1598 字,大约阅读时间需要 5 分钟。
前话
React的函数绑定一致是个问题,主要有下面几种方式:
- 事件处理器动态绑定
export default class Com extends React.Component { render() { }}
- 构造函数绑定
export default class CartItem extends React.Component { constructor(props) { super(props); this.method = this.method.bind(this); } render() { }}
- 构造函数 + 箭头函数 | 箭头函数
export default class CartItem extends React.Component { const method2 = () =>{...} constructor(props) { super(props); this.method = (ev) => {...} } render() { }}
- 通过bael的
这个是babel支持的,还不是标准
export default class CartItem extends React.Component { method = () => {...}; render() { }}
- stage 0 的
export default class CartItem extends React.Component { method(){...}; render() { }}
最后一种很帅气, 然并软,我使用就直接报错。 臣不服,不服。
于是寻找方案, 因为是create-react-app创建的项目。我的思考方案如下- create-react-app内置支持的配置在awesome-create-react-app中找到, 看到了光芒,光芒啊。高兴太早,死的也早,被拒绝了,大致是,想法非常好,非常好,就是不能。那么,我走下一条路
- npm run eject采用eject后,会多出很多文件,并且是不可逆向的。恶心,恶心,那么多文件。我要出去透透气。
- react-app-rewired这个比较简单一些。寻寻觅觅找到了然后找到对应的插件,
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; }}
修改完毕,启动,哦,可以。 真是不错。
扔掉键盘,甩开鼠标,深深的一口水,想写行代码咋这么难。发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月12日 17时04分14秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MongoDB 快速扫盲贴
2021-05-08
one + two = 3
2021-05-08
sctf_2019_easy_heap
2021-05-09
PyQt5之音乐播放器
2021-05-09
Redis进阶实践之十八 使用管道模式提高Redis查询的速度
2021-05-09
SQL注入
2021-05-09
MPI Maelstrom POJ - 1502 ⭐⭐ 【Dijkstra裸题】
2021-05-09
Problem 330A - Cakeminator (思维)
2021-05-09
LeetCode75 颜色分类 (三路快排C++实现与应用)
2021-05-09
C语言+easyX图形库的推箱子实现
2021-05-09
调试vs2019代码的流程
2021-05-09
脱壳与加壳-加壳-6-代码实现加密导入表
2021-05-09
Typora配置PicGo时,提示Failed to fetch
2021-05-09
bcolz的新操作
2021-05-09
zmq的send
2021-05-09
阿里钉钉面试题
2021-05-09
C++中找资源或者函数的方法
2021-05-09
delete对象时会自动调用类的析构函数
2021-05-09
POD类型
2021-05-09
const与常量,傻傻分不清楚~
2021-05-09