简体   繁体   English

如何在 React JS 中的两个页面之间导航

[英]How to navigate between two pages in React JS

I am using React-Router-Dom to navigate between two javascript elements in a create-react-app dapp but when I use it loads the elements on top of the page and just adds the page to the end of the link (localhost/page).我正在使用 React-Router-Dom 在 create-react-app dapp 中的两个 javascript 元素之间导航,但是当我使用它时,它会将元素加载到页面顶部,并将页面添加到链接的末尾(localhost/page ).

My goal is to have the page reload to the new page on click of the link button with a new background and styling (already done for that page)我的目标是在单击具有新背景和样式的链接按钮时将页面重新加载到新页面(已为该页面完成)

App.js Code App.js代码

<Router>
  <s.Screen>
    <Navbar />
    <Routes>
      <Route path="/MyPandas" element={MyPandas} exact></Route>
    </Routes>
          
    /*
      Rest of Code
    */
</Router>

My other page is just wrapped in a Fragment Component我的其他页面只是包裹在一个片段组件中

<Fragment>
  /*
    Code
  */
</Fragment>

 

    

use exact in your Routes eg: <Route path='/products' exact>在您的路线中使用 exact,例如: <Route path='/products' exact>

Assuming your links are working correctly, and it seems they are if the URL in the address bar updates to "/MyPandas" .假设您的链接工作正常,如果地址栏中的 URL 更新为"/MyPandas" ,它们似乎是正确的。

The issue is that the element prop isn't passed a correct value.问题是element属性没有传递正确的值。 react-router-dom@6 Route component's element prop takes a ReactNode , aka JSX, not a reference to a React component. react-router-dom@6 Route组件的element属性采用ReactNode ,又名 JSX,而不是对 React 组件的引用。 Pass <MyPandas /> instead of MyPandas .传递<MyPandas />而不是MyPandas

<Route path="/MyPandas" element={<MyPandas />} />

Note: There's no need for the exact prop.注意:不需要exact道具。 Not only does it not exist in the RRDv6 Route component API, but all routes are now always exactly matched.它不仅不存在于 RRDv6 Route组件 API 中,而且所有路由现在始终完全匹配。

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

相关问题 在 React js 中的页面之间导航 - Navigate between pages in React js 如何在离子的两个页面之间导航 - how to navigate between two pages in ionic 在 React js 中在页面之间导航和处理后退按钮的最佳方式 - Best way to navigate between pages and handle back button in react js 如何使用 JS 在不同的 HTML 页面之间导航 - How to navigate between different HTML pages using JS 如何使用js在两个外部页面之间进行通信 - How to communicate between two external pages with js 如何使用 Firebase (JS) 在 web 上的实时数据库中的两个日期之间导航? - How to Navigate between two dates in Realtime Database on the web with Firebase (JS)? React Native-如何在页面之间导航-错误:未定义不是函数(在附近,&#39;…(0,_reactNAvigation.StackNavigator)...&#39;) - React Native - how to navigate between pages - Error: undefined is not a function(near, '…(0, _reactNAvigation.StackNavigator)…')) 在两个PHP文件或页面之间导航,而无需重新加载 - Navigate between two PHP files or pages without reloading React JS如何使用会话存储在页面之间传递值 - React JS how to pass values between pages using session storage 如何使用js在两个aspx页面之间进行通信? - How to communicate between two aspx pages using js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM