简体   繁体   English

确保上游模块使用正确的browser-react-router的历史记录?

[英]Ensuring upstream module uses correct browserHistory from react-router?

I am running into an issue whereby my the browserHistory.push() operation is not working when in an upstream module. 我遇到一个问题,当我在上游模块中时, browserHistory.push()操作不起作用。 What happens in that the URL is the address bar updates, but the contents of the page don't change. URL发生的是地址栏更新,但是页面的内容没有改变。 Both upstream and downstream modules are using react-router version 3.0.5. 上游和下游模块都使用3.0.5版本的react-router。

The test component in the upstream module is: 上游模块中的测试组件是:

import React from 'react';
import { browserHistory } from 'react-router';

export default function render (props) {
    return (
        <div className="container">
            <div><div style={{height: '200px'}}></div><div onClick={(event) => {browserHistory.push('/login')}}>click me</div></div>                
        </div>
    )
}

The upstream module specifies "react-router": "^3.0.5" in the dependencies section of package.json . 上游模块在package.jsondependencies部分中指定"react-router": "^3.0.5"

A work-around that I have found is to pass browserHistory in as a property, such that in downstream: 我发现一种解决方法是将browserHistory作为属性传递,例如在下游:

 import MyView from 'MyModule';
 import  {browserHistory } from 'react-router';

 <MyView browserHistory={browserHistory} />

and then in upstream: 然后在上游:

 this.props.browserHistory.push('/login')

Note, as part of my analysis I had placed the following code in the downstream module, to see if location changes were being triggered, but I found it wasn't firing until I used my workaround: 请注意,作为分析的一部分,我在下游模块中放置了以下代码,以查看是否触发了位置更改,但是直到使用解决方法后,它才触发:

browserHistory.listen((location, action) => {
    console.log('location change:', location, action);
})

I am not sure that passing browserHistory to the upstream module in this way the right approach, so can anyone tell me whether this is the right approach or whether there is potentially something else causing this behaviour? 我不确定以这种方式将browserHistory传递给上游模块是正确的方法,因此谁能告诉我这是正确的方法还是是否有其他原因导致这种行为?

If I base myself on current behaviour, and what I have observed in the code for the ' Link ' component, it would seem importing browserHistory would not be the correct way to go. 如果我根据当前的行为以及“ 链接 ”组件的代码中所观察到的内容,似乎导入browserHistory并不是正确的方法。 Instead we should probably use the router property that is passed in by react-router, and do the following: 相反,我们可能应该使用react-router传递的router属性,并执行以下操作:

this.props.router.push('/login')

This seems to be the only reliable approach I have found, without having to explicitly pass in browserHistory from the downstream component. 这似乎是我找到的唯一可靠的方法,而不必从下游组件中显式传递browserHistory

The added bonus is that I don't have to worry about what history implementation the router is using, from the component's perspective. 从组件的角度来看,额外的好处是,我不必担心路由器使用的历史实现。

Note, if this could was to be distributed, then it probably shouldn't assume that the downstream module is using react-router, and handle the page change with a fallback approach, such as via window.location or a passed in event handler. 注意,如果可以分发,则可能不应该假设下游模块正在使用react-router,并使用后备方法(例如, via window.location或传入的事件处理程序)来处理页面更改。

BTW as reference, there is this explanation in the react-router lessons: ' Navigating Programatically '. 顺便说一句,作为参考,在React-Router课程中有以下解释:“以编程方式导航 ”。 Also, since browserHistory acts as a form of wrapper to the browser's history API , then it worth noting that in the docs it says history.pushState() will update the URL in the address bar "but won't cause the browser to load" the page, so it is important to consider what events are causing React router to update the displayed page. 另外,由于browserHistory充当了浏览器的历史记录API的包装形式,因此值得注意的是,在文档中说history.pushState()将更新地址栏中的URL“但不会导致浏览器加载”页面,因此重要的是要考虑导致React Router更新显示页面的事件。

Update there may be another cause to this issue: a duplicate version of react-router being included in the lib. 更新可能会导致此问题的另一原因:lib中包含react-router的重复版本。 I only came to this conclusion based on some issues with a duplicate version of react causing issues. 我只基于具有重复版本的一些问题得出了这个结论react造成的问题。 In our case we are using webpack and it was including react in the generated library file. 在我们的例子中,我们使用的是webpack,它包含了react在生成的库文件中。 To resolve this, we need to add the following to the webpack.config.js : 要解决此问题,我们需要将以下内容添加到webpack.config.js

externals: ['react', 'react-dom', 'react-router']

At the same time, one benefit of the this.props.router approach is that we don't need to explicitly include react-router as a dependency. 同时, this.props.router方法的一个好处是我们不需要显式地将react-router包含为依赖项。 I would consider that both these approaches are complementary. 我认为这两种方法是互补的。

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

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