简体   繁体   English

只渲染 react-route 的第一页

[英]Only the first page of react-route is rendered

this is my second using stackflow, so sorry if something is hard to understand, I'm creating a React project, and when I put the Route, only the first page is rendered, and no more, my site is practically One Page, only one page is not the About Page.这是我第二次使用 stackflow,如果有些东西难以理解,很抱歉,我正在创建一个 React 项目,当我放置 Route 时,只呈现第一页,仅此而已,我的网站实际上是一个页面,只有一页不是关于页面。

App.js应用程序.js

const App = () => {
 
  return (
    <>
      <Routes>
        <Route index path="/" element={<Home />} />
        <Route exact path="/Cardapio" element={<Cardapio />} />
        <Route exact path="/Knowmore" element={<Knowmore />} />
        <Route exact path="/About" element={<About />} />
        <Route path="*" element={<p>Página não encontrada</p>} />
      </Routes>   
    </>
  );
};
export default App;

Try this one试试这个

const App = () => {
  return (
    <BrowserRouter>
      <Routes>
         <Route index element={<Home />} />
         <Route path="Cardapio" element={<Cardapio />} />
         <Route path="Knowmore" element={<Knowmore />} />
         <Route path="About" element={<About/>} />
         <Route path="*" element={<NoPage />} />
      </Routes>
    </BrowserRouter>
  );
}

if it is not working, please let me know.如果它不起作用,请告诉我。 It can be related with react version v17 or v18它可以与反应版本v17v18相关

If you want to render several of the components together on the same route they should all be combined and merged on the same path.如果您想在同一条路径上一起渲染多个组件,则应将它们全部组合并合并到同一条路径上。

Example:例子:

<Routes>
  <Route
    path="/"
    element={
      <>
        <Home />
        <Cardapio />
        <Knowmore />
      </>
    }
  />
  <Route path="/About" element={<About />} />
  <Route path="*" element={<p>Página não encontrada</p>} />
</Routes>

If you are wanting to link to the individual components, ie home, cardapio, and know more "sections", then assign appropriate element id attributes to them and target them with rendered raw anchor tags.如果您想链接到各个组件,即 home、cardapio 并了解更多“部分”,则为它们分配适当的元素id属性并使用渲染的原始锚标记定位它们。

Basic example:基本示例:

const Home = () => (
  <div id="home">
    Home
  </div>
);

const Cardapio = () => (
  <div id="cardapio">
    Cardapio
  </div>
);

const Knowmore = () => (
  <div id="knowmore">
    Knowmore
  </div>
);

Nav links in App : App中的导航链接:

<nav>
  <ul>
    <li>
      <a href="#home">Home</a>
    </li>
    <li>
      <a href="#cardapio">Cardapio</a>
    </li>
    <li>
      <a href="#knowmore">Know More</a>
    </li>
    <li>
      <Link to="/about">About</Link>
    </li>
  </ul>
</nav>

仅编辑react-route-is-rendered 的第一页

Try wrapping the content with <BrowserRouter> tag.尝试使用<BrowserRouter>标签包装内容。

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

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