简体   繁体   English

反应路由器 - URL 更改但不是组件

[英]React Router - URL changes but not Component

I have got an issue with React Router, when i am running the app the main page shows but the others not render, I am using some library for the Navigation bar that called react-responsive-animate-navbar, the Nav bar is working properly and the URL is change when i click but the component that i specify for the path/url is not render.我遇到了 React Router 的问题,当我运行应用程序时,主页显示但其他页面未呈现,我正在使用一些名为 react-responsive-animate-navbar 的导航栏库,导航栏工作正常当我单击时,URL 会发生变化,但我为路径/url 指定的组件未呈现。 I tried a couple of solutions that i found on stackoverflow and github and none of them work.我尝试了几个在 stackoverflow 和 github 上找到的解决方案,但没有一个有效。

Code:代码:

App.js应用程序.js

import React, { useState } from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import NavigationBar from "../NavigationBar/NavigationBar";
import Menu from "../Menu/Menu";
import Login from "../Login/Login";
import UpdateUser from "../UpdateUser/UpdateUser";

const App = () => {
const [token, setToken] = useState(1);

return (
    <Router>
        <NavigationBar token={token} />
        <Switch>
            <Route exact path="/" component={token >= 0 ? Menu : () => <Login token={token} setToken={setToken}/>} />
            <Route path="/update" component={() => <UpdateUser />} />
        </Switch>
    </Router>
   );
};

NavigationBar导航栏

import React from "react";
import { ReactNavbar } from "react-responsive-animate-navbar";
import "./NavigationBar.css";

const NavigationBar = ({ token }) => {
   return (
       <ReactNavbar
        color="#000"
        logo="../../styles/images/logo.png"
        menu={[
            { name: "דף הבית", to: "/" },
            { name: "מדריך", to: "/guide" },
            { name: "עדכון משתמש", to: "/update" },
            { name: "התנתק", to: "/contact" },
        ]}
    />
   );
};

export default NavigationBar;

UPDATE:更新:

I figure out that when i change the url and refresh the page is rendered, otherwise nothing happend on clicking and the url is only changing.我发现当我更改 url 并刷新页面时,会呈现,否则单击时没有任何反应,url 只会发生变化。 How can i make it render without the need to refresh?我怎样才能让它在不需要刷新的情况下渲染?

Thank you all in advance !谢谢大家 !

Looks like there was an open issue with the react-responsive-animate-navbar where the navigation didn't work and was fixed by adding a component field to the menu array like so:看起来react-responsive-animate-navbar存在一个未解决的问题,其中导航不起作用,并通过向menu数组添加组件字段来修复,如下所示:

  <ReactNavbar menu={[
            { name: "HOME", to: "/", component: Home },
          ]}
    />

however this fix was not published to npm yet, so you still have the bugged code without the fix mentioned above.但是这个修复还没有发布到 npm ,所以你仍然有错误的代码没有上面提到的修复 I recommend making your own custom ReactNavBar component since it isn't alot of code and the "react-responsive-animate-navbar" library doesn't look well maintained.我建议制作您自己的自定义ReactNavBar组件,因为它的代码不多,而且“react-responsive-animate-navbar”库看起来维护得不好。

I've made a small codepen where I copy/pasted/installed things you would need to begin making your own custom "react-responsive-animate-navbar".我制作了一个小代码笔,在其中复制/粘贴/安装了您开始制作自己的自定义“react-responsive-animate-navbar”所需的东西。 Its all in the ReactNavBar folder.它都在ReactNavBar文件夹中。

I'm not sure this is the fix but if you are going to use an arrow function to render the component you need to change the component prop to render .我不确定这是修复方法,但如果您要使用箭头 function 来渲染组件,您需要将component属性更改为render

When you use component (instead of render or children, below) the router uses React.createElement to create a new React element from the given component.当你使用组件(而不是渲染或子,下面)时,路由器使用 React.createElement 从给定组件创建一个新的 React 元素。 That means if you provide an inline function to the component prop, you would create a new component every render.这意味着如果您为组件属性提供内联 function,您将在每次渲染时创建一个新组件。 This results in the existing component unmounting and the new component mounting instead of just updating the existing component.这会导致现有组件卸载和新组件安装,而不是仅仅更新现有组件。 When using an inline function for inline rendering, use the render or the children prop (below).当使用内联 function 进行内联渲染时,请使用 render 或 children 道具(如下)。

https://reactrouter.com/web/api/Route/component https://reactrouter.com/web/api/Route/component

You'll probably want to change up that function in root path.您可能想要更改根路径中的 function 。

        <Switch>
            <Route exact path="/" render={() => token >= 0 ? <Menu /> : <Login token={token} setToken={setToken}/>} />
            <Route path="/update" render={() => <UpdateUser />} />
        </Switch>

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

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