函数在.then内部之前完成

时间:2019-04-14 19:36:13

标签: reactjs optimizely

所以我正在用以前从未使用过的语言(优化和React)完成任务。

如下所示,根据返回的值,我有2种不同的if语句,返回的值很好。我可以在调试时看到这很好,因此,Optimizely方面似乎可以正常工作。似乎正在发生的事情是主要功能在.then完成之前就结束了,从而导致错误:

Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in. Check the render method of `Home`.

因此,我需要尝试让if语句在函数完成之前运行。

任何帮助将不胜感激,其最终目标是2个为2个不同的用户运行2位代码。

(function(module, exports, __webpack_require__) {

'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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; }; }();

var _react = __webpack_require__(327);

var _react2 = _interopRequireDefault(_react);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

//If original set to 874 if not then set to 884
            var _enums = __webpack_require__(623);
            var _enums2 = _interopRequireDefault(_enums);
            var uuidv4 = __webpack_require__(676);
            var user = { key: uuidv4() }
            var _optimizely_manager = __webpack_require__(631);
            var _optimizely_manager2 = _interopRequireDefault(_optimizely_manager);
            _optimizely_manager2.default.getInstance().then(function (optimizelyInstance) {
                const variation = optimizelyInstance.activate(_enums2.default.EXPERIMENT_KEYS.CAT_ABOVE_TITLE, user.key)
                if(variation === _enums2.default.VARIATION_KEYS.ABOVE_TITLE_ORIGINAL){
                    var _item_list_item = __webpack_require__(874);
                    var _item_list_item2 = _interopRequireDefault(_item_list_item);

                    var ShoeListComponent = function (_React$Component) {
                        _inherits(ShoeListComponent, _React$Component);

                        function ShoeListComponent() {
                            _classCallCheck(this, ShoeListComponent);

                            return _possibleConstructorReturn(this, (ShoeListComponent.__proto__ || Object.getPrototypeOf(ShoeListComponent)).apply(this, arguments));
                        }

                        _createClass(ShoeListComponent, [{
                            key: 'render',
                            value: function render() {
                                var items = this.props.items;
                                return _react2.default.createElement(
                                    'ul',
                                    null,
                                    Object.keys(items).map(function (itemId) {
                                        return _react2.default.createElement(_item_list_item2.default, {
                                            key: itemId,
                                            item: items[itemId]
                                        });
                                    })
                                );
                            }
                        }]);

                        return ShoeListComponent;
                    }(_react2.default.Component);

                    exports.default = ShoeListComponent;
                }
                else if(variation === _enums2.default.VARIATION_KEYS.ABOVE_TITLE_VARIATION){
                    var _item_list_item = __webpack_require__(884);
                    var _item_list_item2 = _interopRequireDefault(_item_list_item);

                    var ShoeListComponent = function (_React$Component) {
                        _inherits(ShoeListComponent, _React$Component);

                        function ShoeListComponent() {
                            _classCallCheck(this, ShoeListComponent);

                            return _possibleConstructorReturn(this, (ShoeListComponent.__proto__ || Object.getPrototypeOf(ShoeListComponent)).apply(this, arguments));
                        }

                        _createClass(ShoeListComponent, [{
                            key: 'render',
                            value: function render() {
                                var items = this.props.items;
                                return _react2.default.createElement(
                                    'ul',
                                    null,
                                    Object.keys(items).map(function (itemId) {
                                        return _react2.default.createElement(_item_list_item2.default, {
                                            key: itemId,
                                            item: items[itemId]
                                        });
                                    })
                                );
                            }
                        }]);

                        return ShoeListComponent;
                    }(_react2.default.Component);

                    exports.default = ShoeListComponent;
                }
            });
}), 

这是我正在使用的演示应用程序。

https://github.com/optimizely/isomorphic-react-demo-app

任务是什么,任务是实际修改视图的顺序。因此,一个人将看到视图A,一个人将看到视图B,因此874的if语句是原始代码,而884是我使用874并对其进行修改以查看他们想要的代码。我知道外观是正确的,好像我在874中进行了更改一样,将视图更改为我想要的样子。我只是有两个选项可能都返回的问题。

我什至可能在做错树上做事,但这就是我以为会做的事。

1 个答案:

答案 0 :(得分:1)

由于您刚刚开始使用Optimizely和React,因此建议您遵循React quickstart for Optimizely Rollouts。这使用了Optimizely React SDK,这使您可以轻松地在React应用程序中呈现两个不同的组件,就像您在上面所做的那样。如果您没有Optimizely帐户,则可以创建一个Optimizely Rollouts account

如果您想继续使用isomorphic-react-demo-app,请确保您的实验为每个变体分配了50%的流量,并且变体键与在Optimizely项目中创建的键相匹配。此外,由于uuidv4()函数每次都会返回一个新的随机字符串,并且每个ID都会在变体中随机存储,因此您可能必须多次重新加载isomorphic-react-demo-app才能看到这两个变体。

提供指向数据文件的链接并为变体键提供代码会有所帮助,但是如果您是Optimizely + React的新手,我建议从上述快速入门入手。

希望这会有所帮助!