[英]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.