简体   繁体   English

在transitionTo(Chrome)上意外加载React-Router

[英]React-router unintended reload on transitionTo (Chrome)

In a (single-page) application implemented on top of React and React Router, we are observing a strange pattern when using the router.transitionTo method. 在React和React Router之上实现的(单页)应用程序中,当使用router.transitionTo方法时,我们观察到一个奇怪的模式。

Apologies for the extra verbose context, but don't want to miss something pertinent. 对于额外的冗长内容,我们深表歉意,但不想错过相关内容。

Let's assume that we have the following Router initialization, which is called from the single "physical" page of the application: 假设我们具有以下路由器初始化,可从应用程序的单个“物理”页面调用该初始化:

define([], function () {
    var _Root = React.createClass({
            render : function () {
                return React.createElement(React.addons.CSSTransitionGroup, {/**/},
                React.createElement(Router.RouteHandler, {/**/}));
            }
        });
    var _NotFoundScreen = React.createClass({/**/});
    var _NoDefaultScreen = React.createClass({/**/});
    var routes = (
        React.createElement(Router.Route, {
            handler : _Root,
            path : "/SampleApplication/"
        },
        React.createElement(Router.Route, {
            path : "Transfer",
            handler : Transfer
        }),
        React.createElement(Router.Route, {
            path : "Home",
            handler : Home
        }),
        React.createElement(Router.DefaultRoute, {
            handler : Home || _NoDefaultScreen
        }),
        React.createElement(Router.NotFoundRoute, {
            handler : _NotFoundScreen
        })));
    return {
        init : function () {
            var router = Router.create({
                routes : routes,
                location : Router.HistoryLocation
            });
            router.run(function (rootClass, state) {
                if (state.routes.length === 0) {
                    window.location = state.path;
                }
                React.render(React.createElement(rootClass, state), document.getElementById("reactContainer"));
            });
        }
    }
});

And then we have the "Transfer" page with something along the lines of: 然后,我们进入“ Transfer”页面,其中包含以下内容:

define([], function () {
    var View = (function (_super) {
        /*(...)*/
        function View() {
            /*(...)*/
        }
        View.prototype.render = function () {
            /*(...)*/
            return React.DOM.div(null, React.createElement(Button.Button, {
                    enabled : true,
                    onClick : function () {
                        router.transitionTo("/SampleApplication/Home");
                    },
                    style : "btn",
                    visible : true
                }, "B1"), React.DOM.br(), "form:", React.createElement(Form.Form, {
                    style : "form",
                    visible : true
                }, React.createElement(Button.Button, {
                        enabled : true,
                        onClick : function () {
                            router.transitionTo("/SampleApplication/Home");
                        },
                        style : "btn btn-primary",
                        visible : true
                    }, "B2")));
        };
        return View;
    })(/*(...)*/);
    return View;
});

So, we have 2 buttons on the Transfer page - B1 and B2. 因此,我们在“ 传输”页面上有2个按钮-B1和B2。 B2 is wrapped by a "form" element. B2由“表单”元素包裹。 Both buttons are capable of "navigating" to the home page, when clicked. 单击两个按钮都可以“导航”到主页。

Navigation through B1 works as expected. 通过B1导航按预期方式工作。

Navigation through B2 reveals some peculiar behavior. 通过B2导航显示一些特殊行为。

  • browser url: host/SampleApplication/Transfer 浏览器网址:host / SampleApplication / Transfer
  • we click B2 我们单击B2
  • we "navigate" to host/SampleApplication/Home and see the page content for a fraction of a second 我们“导航”到主机/ SampleApplication / Home,并在不到一秒钟的时间内看到页面内容
  • fraction of a second later, browser url changes to host/SampleApplication/Home? 几分之一秒后,浏览器url更改为host / SampleApplication / Home?
  • we get a white screen (as if we're loading/accessing the application for the first time and it is initializing) 我们得到一个白屏(好像我们是第一次加载/访问该应用程序并且它正在初始化)
  • we get the rendered page @ host/SampleApplication/Home? 我们得到渲染的页面@ host / SampleApplication / Home?

I have been trying to find the issue for a while now and no amount of debugging seems to be producing any results. 我已经尝试查找问题已有一段时间了,并且似乎没有大量调试产生任何结果。

The execution flows for the navigation from both B1 and B2 are identical (down to the point where the React Router calls location.push(path)). 从B1和B2导航的执行流程是相同的(直到React Router调用location.push(path)为止)。

Furthermore, this "only" happens with Chrome (desktop and mobile), Opera (mobile) and Android stock browser, while for Firefox (desktop and mobile) both B1 and B2 are able to navigate from one page to another without any extra reloads nor "leaving" the single physical page of the application. 此外,Chrome浏览器(台式机和移动设备),Opera(移动设备)和Android股票浏览器仅发生这种情况,而对于Firefox(台式机和移动设备)来说,B1和B2都可以从一页导航到另一页,而无需进行任何重新加载“离开”应用程序的单个物理页面。

I have been unable to find any pertinent information about similar behavior patterns that could explain what could be going on. 我一直找不到类似行为模式的任何相关信息,这些信息可以解释可能发生的情况。

If anyone could provide some insight on what could be happening here, it would be most appreciated. 如果有人可以提供一些有关此处可能发生的情况的见解,将不胜感激。

With best regards, 最诚挚的问候,

SYG SYG

Ok, the source of the problem was identified as being the default type of button element - submit ( link to the HTML recommendation). 好的,问题的根源被确定为按钮元素的默认类型 - 提交链接到HTML建议)。

Since the button was being rendered within a form element, the "onClick" of the button was implicitly submitting the form, causing a page reload to get queued. 由于按钮是在表单元素中呈现的,因此按钮的“ onClick”将隐式提交表单,从而导致页面重新加载进入排队状态。 The "onClick" of the button executes router.transitionTo, effectively navigating to the target page and the "queued" page reload from the form submit gets executed immediately afterwards, causing the application to re-initialize. 按钮的“ onClick”执行router.transitionTo,有效地导航到目标页面,然后从表单提交重新加载的“排队”页面立即执行,从而导致应用程序重新初始化。

The behavior was "fixed" by changing the type of the button element to button . 的行为是“固定”,通过改变按钮元件到按钮的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM