在create-react-app创建的项目下允许函数绑定运算符
2018-07-12 14:25
841 查看
前话
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 { 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脚手架创建react项目,react的基本语法,以及react中的事件绑定
- Create React App快速创建React项目
- 在create-react-app创建的React项目应用中配置JQ、Sass
- create-react-app如何创建react项目?
- 以create-react-app为基础创建项目
- react以create-react-app为基础创建项目
- 使用create-react-app或者antd创建一个React项目
- create-react-app创建react项目,使用axios跨域
- react---学习笔记:使用create-react-app快速构建开发环境并创建项目
- 以create-react-app为基础创建项目
- create-react-app创建react项目失败!
- Create-React-App创建React项目
- 使用create-react-app创建项目
- create-react-app 创建项目失败
- create-react-app创建项目报错SyntaxError: Unexpected end of JSON input while parsing near '...ttachment":false,"tar' npm代理
- 使用create-react-app自动构建react项目
- 用weex create 创建app项目 报 ERROR in index.web.js from UglifyJs 错误!
- creact-react-app创建项目跨域的处理
- Create-React-App创建antd-mobile开发环境
- 使用create-reate-app创建的项目中代理的配置