Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
How to hide the Footer component at a specific URL

Code: https://codesandbox.io/s/aboutlayout-o3gmd?file=/src/App.js

In the Header are Routes, when you go to /cabinet render component <Cabinet/>, I need to make it so that when you are on the route /cabinet component <Footer /> should not be rendered

Tried to get the current path using hook const match = useRouteMatch(), but it must be called inside the component <Cabinet/>, but the path I need in the component <App/>, to make a condition, {match !== /cabinet && <Footer />} how do I get the current path is in the <App/> component?

I also wanted to use const match = useRef(window.location.pathname); but it doesn't update

<App/> component :

import { Redirect, Route, Switch } from "react-router-dom";
import { BrowserRouter, useRouteMatch } from "react-router-dom";
import "./styles.css";
import { Home } from "./pages/Home";
import { Catalog } from "./pages/Catalog";
import { Cabinet } from "./pages/Cabinet";
import { Header } from "./components/Header";
import { Footer } from "./components/Footer";
import { Page404 } from "./pages/Page404";
import { useRef, useState } from "react";

const App = () => {
  const match = useRef(window.location.pathname);
  // const [path, setPath] = useState(window.location.pathname)

  console.log(match);
  return (
    <>
      <BrowserRouter>
        <Header />
        <Switch>
          <Route path={"/"} exact>
            <Home />
          </Route>
          <Route path={"/catalog"} exact>
            <Catalog />
          </Route>
          <Route path={"/cabinet"} exact>
            <Cabinet />
          </Route>
          <Route path={"*"}>
            <Page404 />
          </Route>
          {/* <Redirect to={'/'} /> */}
        </Switch>
        <Footer />

        {/*
        {true && <Footer />}  // will be rendered
        {false && <Footer />} // will not be rendered
        {(match !== /cabinet) && <Footer />} 
        */}
      </BrowserRouter>
    </>
  );
};

export { App };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use useLocation() hook to check the route path in the <Footer /> component.

Example :

import React from "react";
import { useLocation } from "react-router-dom";

const Footer = ({ path }) => {
  const { pathname } = useLocation();
  console.log(pathname);
  if (pathname === "/cabinet") return null;

  return <div className="footer">Footer</div>;
};
export { Footer };

App.js

import { Route, Switch } from "react-router-dom";
import { BrowserRouter } from "react-router-dom";
import "./styles.css";
import { Home } from "./pages/Home";
import { Catalog } from "./pages/Catalog";
import { Cabinet } from "./pages/Cabinet";
import { Header } from "./components/Header";
import { Footer } from "./components/Footer";
import { Page404 } from "./pages/Page404";

const App = () => {

  return (
    <>
      <BrowserRouter>
        <Header />
        <Switch>
          <Route path={"/"} exact>
            <Home />
          </Route>
          <Route path={"/catalog"} exact>
            <Catalog />
          </Route>
          <Route path={"/cabinet"} exact>
            <Cabinet />
          </Route>
          <Route path={"*"}>
            <Page404 />
          </Route>
          {/* <Redirect to={'/'} /> */}
        </Switch>
        <Footer />
      </BrowserRouter>
    </>
  );
};

export { App };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!