雖然過了兼容IE6的噩夢時代,IE依舊陰魂不散,由於你可能還要兼容IE9。在ES6已經普及的今天,用ES6寫react已經成了標配。可是babel編譯的js語法,因爲某些不規範的寫法,可能在IE9下不能正確解釋,很容易致使白屏。本文記錄以下react
在準備提測的那天,順便打開IE9看一眼(注意,這裏是原生IE9 ,不是用IE11模擬的IE9),OMG!es6
排查後發現,原來是由於構造函數中使用了this
。簡寫以下express
class Child extends React.Component { constructor(props) { super(props); this.state = {count:this.props.count} } render(){ return (<p>child</p>) } } class Superer extends React.Component { state = {count:1} render() { return <Child count = {this.state.count}/> } }
老司機們確定能一眼發現問題:this.state = {count:this.props.count}
構造函數中不該該使用this
,而是 super(props)
傳入的 porps
,應該改成this.state = {count:props.count}
. 改正以後,問題確實解決了。可是問題來了,雖然寫法確實不規範,爲何其餘瀏覽器都運行正常,包括IE11,用IE11模擬iE9也沒有問題,恰恰就原版的IE9有問題。segmentfault
怎麼能就這麼不明不白的算了,哼!瀏覽器
既然瀏覽器運行的代碼是通過babel編譯的,那這個鍋先甩給babel。查看一下babel編譯後的源碼。以下babel
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Child = function (_React$Component) { _inherits(Child, _React$Component); function Child(props) { _classCallCheck(this, Child); var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, props)); _this.state = { count: _this.props.count }; return _this; } _createClass(Child, [{ key: "render", value: function render() { return React.createElement( "p", null, "child" ); } }]); return Child; }(React.Component); var Superer = function (_React$Component2) { _inherits(Superer, _React$Component2); function Superer() { var _ref; var _temp, _this2, _ret; _classCallCheck(this, Superer); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this2 = _possibleConstructorReturn(this, (_ref = Superer.__proto__ || Object.getPrototypeOf(Superer)).call.apply(_ref, [this].concat(args))), _this2), _this2.state = { count: 1 }, _temp), _possibleConstructorReturn(_this2, _ret); } _createClass(Superer, [{ key: "render", value: function render() { return React.createElement(Child, { count: this.state.count }); } }]); return Superer; }(React.Component);
重點看_inherits()
和Child構造函數,app
subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; function Child(props) { _classCallCheck(this, Child); var _this = _possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this, props)); _this.state = { count: _this.props.count }; return _this; }
找不到的就是 _this.props.cout
,顯然,_this指向錯誤了。查閱(谷)資料(歌)後發現,getPrototypeOf()
是 ES5 的方法,IE9+ 都能獲得很好的支持,而 setPrototypeOf(),subClass.__proto__ = superClass
是 ES6 的方法,須要到 IE11 才支持,因此_this
其實指向的是Function.prototype
,而不是react.Component
。因此props
沒有成功賦給Child
類,固然就找不到了。函數
果真這個鍋是babel的。this
那要怎麼解決呢?若是是本身寫的邏輯,直接修改寫法就能夠了。可是,若是你用了開源組件,看了源碼,找到問題,提了issue,開發者還跟你互動,就說沒問題,他還說他親測沒問題,就是不改,你該怎麼辦?(手動微笑臉)
固然是原(huan)諒(zu)他(jian)啊~~ ,既然鍋是babel的,那就確定還有一種解決方法。spa
使用babel插件babel-preset-es2015-ie
該插件,在檢測到setPrototypeOf(),subClass.__proto__
不支持時,本身包裝了一個方法
function _inherits(subClass, superClass) { ...; if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); } function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
至此,IE9下老是報錯的問題就解決了,但願能給一樣掉進此坑的小夥伴一點幫助,早點擺脫IE的魔爪。
參考文章:
ES6 + Webpack + React + Babel 如何在低版本瀏覽器上愉快的玩耍(上)
BABEL6 編譯 ES6 繼承代碼的一個兼容問題(IE <= 10)