简体   繁体   English

有没有办法在页面上只留下一个组件和链接的内容?

[英]Is there a way to leave just one component and link's content on the page?

Here is the code:这是代码:

import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Navbar from "./components/navbar/navbar";
import Intro from "./components/intro/intro";
import Services from "./components/services/services";
import Experience from "./components/experience/experience";
import TrelloClone from "./pages/TrelloClone/TrelloClone";
import './App.css'

function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Navbar />
        <Routes>
          {/* <Route path="/vanillajs" element={SpotifyClone} />
          <Route path="/djangoapi" element={InstagramClone} /> */}
          <Route path="/reactjs" element={<TrelloClone />} />
        </Routes>
      </BrowserRouter>
      <Intro />
      <Services />
      <Experience />
    </div>
  );
}

export default App;

So basically I need to hide(or remove) <Intro /> , <Services /> and <Experience /> by clicking the link that leads to ' /reactjs ' path but leave the <Navbar /> on the page.所以基本上我需要隐藏(或删除) <Intro /><Services /><Experience /> ,方法是单击指向“ /reactjs ”路径的链接,但将<Navbar />留在页面上。

edit: grammar and clarifications编辑:语法和说明

install this history from this link , when your package is installed successfully, create a file history.js and put the below code in this file.从此链接安装此history ,当您的软件包安装成功后,创建一个文件 history.js 并将以下代码放入此文件中。

import {createBrowserHistory} from 'history';

export default createBrowserHistory();

import the history file in this file like import history from './history' , then you will get the history props and you can console the history props and check which props are coming and you will find pathname in the history props and make a condition and as you can see in the code below.在此文件中导入历史文件,如import history from './history' ,然后您将获得历史道具,您可以控制台历史道具并检查哪些道具即将到来,您将在历史道具中找到pathname并创建条件正如您在下面的代码中看到的那样。

import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import history from './history'
import Navbar from "./components/navbar/navbar";
import Intro from "./components/intro/intro";
import Services from "./components/services/services";
import Experience from "./components/experience/experience";
import TrelloClone from "./pages/TrelloClone/TrelloClone";
import './App.css'

function App() {
  return (
    <div className="App">
      <BrowserRouter>
        <Navbar />
        <Routes>
          {/* <Route path="/vanillajs" element={SpotifyClone} />
          <Route path="/djangoapi" element={InstagramClone} /> */}
          <Route path="/reactjs" element={<TrelloClone />} />
        </Routes>
      </BrowserRouter>
      {history?.location?.pathname !== '/reactjs' && 
     <>
      <Intro />
      <Services />
      <Experience />
     </>
      }
    </div>
  );
}

export default App;

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

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