简体   繁体   English

如何在 react js 中转到(导航)下一页

[英]how to go (navigate) the next page in react js

I have a page called Home.js which has a button,so when it is clicked it should take me to another page called Land.js but it display the content of both of the pages in similar page ..how to redirect to Land.js without displaying the content of the Home.js??我有一个名为 Home.js 的页面,它有一个按钮,所以当它被点击时,它应该带我到另一个名为 Land.js 的页面,但它在相似的页面中显示两个页面的内容..如何重定向到 Land。 js 而不显示 Home.js 的内容??

this is my app.js这是我的 app.js

import { BrowserRouter, Route } from "react-router-dom";
import Home from "./components/Home";
import Land from "./components/Land";
function App() {
  return (

    <BrowserRouter>
      <div className="App">
        <Home />
        <Route path="/Land" component={Land} />
      </div>

    </BrowserRouter>



  );
}

export default App;

Home.js主页.js

import React from 'react'
import './start.css'
import { Link } from "react-router-dom"


function Home() {

    return (

        <div className="content">
            <h2>MusicQ</h2>
            <h2>MusicQ</h2>
            <Link to="/Land" className="button">Submit</Link>


        </div>

    )
}
export default Home

Land.js陆地js

import React from 'react'

const Land = () => {
    const txt = {
        color: 'black'
    }
    return (
        <div>
            <h1 style={txt}>hello how are you</h1>
        </div>
    )
}

export default Land

so how can we redirect to the next page and display the content to next page??那么我们如何重定向到下一页并将内容显示到下一页? can u please help me to figure this out你能帮我解决这个问题吗

Thank you in advance先感谢您

While React Routers can be used to navigate between components, you could of course also replace the whole screen to make it look like it navigated to a different page.虽然 React Routers 可用于在组件之间导航,但您当然也可以替换整个屏幕,使其看起来像是导航到不同的页面。

You need to route your root path to Home, so something like this should do it -你需要将你的根路径路由到 Home,所以应该这样做 -

import { BrowserRouter, Route } from "react-router-dom";
import { Link, Switch } from "react-router-dom"

function App() {
  return (
    <BrowserRouter>
      <div>
        <Link to="/" >Home</Link>
        <br></br>
        <Link to="/Land">Land</Link>

        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/Land" component={Land} />
        </Switch>
      </div>
    </BrowserRouter>
  );
}

const Home = () => {
  return (
    <div>
      <h2>Home Page</h2>
    </div>
  )
}

const Land = () => {
  return (
    <div>
      <h2>Land Page</h2>
    </div>
  )
}

export default App;

Note : Notice we need to use the exact path, to avoid conflict as / will be a part of every other path.注意:注意我们需要使用确切的路径,以避免冲突,因为/将成为所有其他路径的一部分。

Do also understand that the page does not ever refresh, but the React Router updates the URL as you navigate across the site (as described in the React Router Example Docs ).还要了解页面永远不会刷新,但是当您在站点上导航时,React 路由器会更新 URL(如React Router 示例文档中所述)。 The reason for this is that it preserves the browser history while making sure features such as the back button may work properly.这样做的原因是它保留了浏览器历史记录,同时确保后退按钮等功能可以正常工作。

You need to add Home in a Route :您需要在Route添加 Home :

<Route path="/" component={Home} />
<Route path="/Land" component={Land} />

Under the BrowserRouter put references to all pages.在 BrowserRouter 下放置对所有页面的引用。 then use Link to navigate to any of the page which are referenced in the Route然后使用链接导航到路由中引用的任何页面

/** Make a reference to all the pages **/
    <BrowserRouter>
      <div className="App">
         <Route path="/" component={Home} exact />
        <Route path="/Land" component={Land} />
      </div>
    </BrowserRouter>
    
/** Use Link to navigate anywhere within the page **/
    <Link to="/Land" className="button">go to land</Link>
    <Link to="/Home" className="button">got to Home</Link>

For better understanding refer the React-router Docs为了更好地理解,请参阅React-router Docs

while using react router you need to add all pages in route including home page.if you not adding home into Route then home is act like a parent class after you called anything that also displayed with home page.this problem is you faced now.在使用反应路由器时,您需要在路由中添加所有页面,包括主页。如果您没有将主页添加到路由中,那么在您调用也显示在主页上的任何内容后,主页就像一个父类。这个问题是您现在面临的。 Also use exact in home route to avoid some similar path issues.也使用精确的回家路线以避免一些类似的路径问题。

 <Route path="/" exact> <Home/> </Route> <Route path="/Land"> <Land/> </Route>

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

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