简体   繁体   English

浏览器导航不适用于使用 BrowserRouter 的反应路由器 v5

[英]Browser navigation not working with react router v5 using BrowserRouter

Solved answare below解决了下面的答案

I'm building a web app with react on codesandbox.我正在构建一个 web 应用程序,它对 codesandbox 有反应。 I have 2 navigational components, both functional components, one is the header and the other one in the footer.我有 2 个导航组件,都是功能组件,一个是 header,另一个在页脚中。 I'm using a switch to show different pages between the header and footer.我正在使用一个开关来显示 header 和页脚之间的不同页面。

The navigation changes the URL and the switch does his job showing me the right page, the only problem is that the changes are not updating the history of the browser, or overwrites it maybe???导航更改为 URL,开关会显示正确的页面,唯一的问题是更改不会更新浏览器的历史记录,或者可能会覆盖它??? don't know.不知道。

I already tried coding in a button to call a function that does this:我已经尝试在按钮中编码以调用执行此操作的 function:

history.goBack();

It works as it should but the browser button does not.它可以正常工作,但浏览器按钮没有。 When I navigate with the navigation menu, pages change but the back-button of the browser remains "grey"/"unclickable".当我使用导航菜单导航时,页面发生变化但浏览器的后退按钮保持“灰色”/“不可点击”。

So the question is: How do I get the BROWSER navigation (forward and back button of the browser) working?所以问题是:如何让 BROWSER 导航(浏览器的前进和后退按钮)工作? I learned that BrowserHistory should take care of it without no additional code, am I wrong?我了解到 BrowserHistory 应该在没有额外代码的情况下处理它,我错了吗?

- -

-- Following are just some extra info that may/may not be related/significant -- -- 以下只是一些可能/可能不相关/重要的额外信息 --

I tried using a simple Link on my navigation like this:我尝试在我的导航中使用一个简单的链接,如下所示:

<Link to="/Services">Services</Link>

Also I tried using history.push like this with an onClick handler function:我也尝试像这样使用 history.push 和 onClick 处理程序 function:

const history = useHistory();
function handleChangePage(pageUrl) {
  history.push("/"+pageUrl);
}

On click event that triggers handleChangePage:在触发 handleChangePage 的点击事件上:

<h2 className="headBarMenuItem" onClick={() => handleChangePage( "Services")}>
 Services
</h2>

This is how I have set up the BrowserRouter and the switch inside the app component:这就是我在应用程序组件中设置 BrowserRouter 和开关的方式:

Index.js is set up like this: Index.js 是这样设置的:

<BrowserRouter>
  <App/>
</BrowserRouter>

App (functional component) is set up like this: App(功能组件)设置如下:

<div className="App">
  <Header />
  <div>
    <Switch>
        <Route exact path="/" component={Homepage} />
        <Route path="/Home" component={Homepage} />
        <Route
          path="/Services"
          component={InfluencerMarketingPage}
        />
        <Route path="/Contacts" component={ContactPage} />
        <Route path="/Projects" component={ProjectPage} />
    </Switch>
  </div>
  <Footer />
</div>

Footer Navigation:页脚导航:

<div className="footerMainMenu">
          <h4>
            <Link to="/">Home</Link>
          </h4>
          <h4>
            <Link to="/Projects">Case History</Link>
          </h4>
          <h4>
            <Link to="/Services">Services</Link>
          </h4>
          <h4>
            <Link to="/Contacts">Contacts</Link>
          </h4>
          <h4>
            <Link to="/Aboutus">About Us</Link>
          </h4>
        </div>
                          

Dependecies are: react 16.12, react-dom 16.12, react-router-dom 5.2, react-scripts 3.0.1依赖项是:react 16.12、react-dom 16.12、react-router-dom 5.2、react-scripts 3.0.1

PS I got it working the day before posting this question by just adding react-router 5.2 as a dependency even if I didn't need it, it was working with my footer navigation. PS 我在发布这个问题的前一天通过添加 react-router 5.2 作为依赖项使它工作,即使我不需要它,它正在使用我的页脚导航。 Today I added the header and noticed that the browser navigation was not working anymore, I tried removing and adding react-router as a dependency and then also tried downgrading to react-router-dom 4 but no changes.今天我添加了 header 并注意到浏览器导航不再工作,我尝试删除并添加 react-router 作为依赖项,然后还尝试降级到 react-router-dom 4 但没有变化。 Even if I remove the new header component and do the same testing by adding and removing react-router dependency, still no changes.即使我删除了新的 header 组件并通过添加和删除 react-router 依赖项进行相同的测试,仍然没有任何变化。

Got me thinking maybe I was just dreaming yesterday - Not the case tho.让我想到也许我昨天只是在做梦 - 但事实并非如此。

It looks like you don't have an actual event that triggers your handleChangePage function. Try adding that function to an onClick() on each page where ever the user would click a link to get to a new page.看起来您没有触发 handleChangePage function 的实际事件。尝试将 function 添加到每个页面上的 onClick() 中,用户将单击链接以进入新页面。

Then to handle the forward and back buttons you would add然后处理您要添加的前进和后退按钮

function previousPage() {
history.goBack();

and for the button和按钮

onClick={previousPage}

Same for the forward button you would use与您将使用的前进按钮相同

history.goForward()

So i resolved my problem.所以我解决了我的问题。

The browser buttons are now working on every device/browser, I am 99% sure that the problem was caused by having codesandbox and the full page of the app open in the same chrome window (different tabs but same window).浏览器按钮现在可以在每个设备/浏览器上使用,我 99% 确定问题是由于在同一个 chrome window(不同的选项卡但相同的窗口)中打开 codesandbox 和应用程序的整个页面引起的。

By opening a different window of chrome (NOT different tab) or by opening the web app page in another browser everything works just fine.通过打开不同的 window chrome(不是不同的选项卡)或在另一个浏览器中打开 web 应用程序页面,一切正常。

The problem is ultimately caused by some kind of conflict between codesandbox and the full page of the app in the same window of the chrome browser.问题最终是由于chrome浏览器同一个window中codesandbox和app整页的某种冲突引起的。

PS Looks like a very specific problem but hopefully, it is helpful to someone. PS 看起来是一个非常具体的问题,但希望对某人有所帮助。

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

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