博客
关于我
在create-react-app创建的项目下允许函数绑定运算符
阅读量:415 次
发布时间:2019-03-06

本文共 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;    }}

修改完毕,启动,哦,可以。 真是不错。

扔掉键盘,甩开鼠标,深深的一口水,想写行代码咋这么难。

转载地址:http://fdhkz.baihongyu.com/

你可能感兴趣的文章
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>