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

76
Views
Header Only Appears On One Page

I am creating an Amazon Clone using ReactJS and I am having trouble I think using the "react-router-dom" package.

Here is my App.js file with my lines using the Router function

<Router>
  <div className="app">
    <Routes>
      <Route path="/orders" element={< Header />, < Orders />} />
      <Route path="/login" element={< Login />} />
      <Route path="/checkout" element={< Header />, < Checkout />} />
      <Route path="/payment" stripe={promise} element={ < Payment />} />
      <Route path="/" element={< Header />, < Home />} />
    </Routes>
  </div>
</Router>

Each of the localhost3000 pages works, but the only one that has a header included in the "/payment" page. Some solutions that I have tried are updating react-router from v5 to v6, then I had to undo the switch function and switch it to the routes function. Also, I have tried going into Header.js file and looking for anything that could keep the header from appearing in each of the pages or why it is only showing up on that one page.

Thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The issue is indeed with the value passed to the element prop of the Route components. What you've done is used the comma operator.

element={<Header />, <Orders />} // <-- comma operator

Comma operator (,)

The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions. This is commonly used to provide multiple parameters to a for loop.

In other words, it processed all the operands and returned only the last one, the non-header component.

To resolve you can either return an array of JSX literals

element={[<Header />, <Orders />]}

or return a fragment

element={(
  <>
    <Header />
    <Orders />
  </>
)}

Alternatively, if rendering a header with a page is something you commonly do, abstract this into a layout component.

import { Outlet } from 'react-router-dom';

const HeaderLayout = () => (
  <>
    <Header />
    <Outlet /> // <-- nested routes rendered here
  </>
);

...

<Router>
  <div className="app">
    <Routes>
      <Route element={<HeaderLayout />}>
        <Route path="/orders" element={<Orders />} />
        <Route path="/checkout" element={<Checkout />} />
        <Route path="/" element={<Home />} />
      </Route>
      
      <Route path="/login" element={< Login />} />
      <Route path="/payment" stripe={promise} element={ < Payment />} />
    </Routes>
  </div>
</Router>
about 4 years ago · Juan Pablo Isaza Report

0

I have reasons to believe that you are passing wrongly the components inside each <Route>'s element={...}.

Please try:

  • to pass just one component, not multiples
  • to follow react-router-dom's examples code about how to pass the component

React Router v5 example

Solution 1

<Router>
   <Route path="/orders" component={Orders} />
</Router>

Solution 2

<Router>
   <Route path="/orders">
      <Orders/>
   </Route>
</Router

Read more here on react-router-dom v5 official docs' examples

React Router v6 example

    <Routes>
      <Route path="/" element={<App />}>
        <Route index element={<Home />} />
    </Routes?

Read more here on react-router-dom v6 official docs' example.

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!