0

App.js

import React from "react";
import HomePage from "./pages/homepage/homepage.component";
import ShopPage from "./pages/shop/shop.component";
import Header from "./components/header/header.component";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import "./App.css";

function App() {
  return (
    <div>
      <Router>
        <Header />
        <Switch>
          <Route exact path="/" component={HomePage} />
          <Route path="/shop" component={ShopPage} />
        </Switch>
      </Router>
    </div>
  );
}

export default App;

Header.js

import { withRouter } from "react-router-dom";
const Header = () => (
  <div className="header">
    <Link className="logo-container" to="/">
      <Logo className="logo" />
    </Link>
    <div className="options">
      <Link className="option" to="/shop">
        SHOP
      </Link>
      <Link className="option" to="/shop">
        CONTACT
      </Link>
    </div>
  </div>
);
export default withRouter(Header);

menu-item.jsx

const MenuItem = ({ title, imageUrl, size, history, linkUrl, match }) => (
  <div
    className={`${size} menu-item`}
    onClick={() => history.push(`${match.url} ${linkUrl}`)}
  >
    <div
      className="background-image"
      style={{
        backgroundImage: `url(${imageUrl})`,
      }}
    />
    <div className="content">
      <h1 className="title">{title.toUpperCase()}</h1>
      <span className="subtitle">SHOP NOW</span>
    </div>
  </div>
);
export default MenuItem;

shop.jsx

class ShopPage extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      collections: SHOP_DATA,
    };
  }

  render() {
    const { collections } = this.state;
    return (
      <div className="shop-page">
        {collections.map(({ id, ...otherCollectionProps }) => (
          <CollectionPreview key={id} {...otherCollectionProps} />
        ))}
      </div>
    );
  }
}

export default ShopPage;

Here's my problem. My entire Navigation panel is at Header.js. Whenever I click the "Shop" link I expect it would load that page. But after clicking on shop at the browser's URL bar it is showing the correct link though not getting to load. When I reload the page or manually type and press enter only then does the ShopPage's content is getting loaded but not on clicking...


I am using react-router-v5 Can anyone please help to solve this issue? This is my first question on this platform so please pardon me If I made any mistake with my question or if I failed to make you guys understand my problem. TIA

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
Siam
  • 1
  • In app.js maybe try putting as the top level element? Outside the
    – Grambam May 18 '22 at 22:13
  • This is a known issue between `react-router@5`/`react-router-dom@5` and `react@18` and the `React.StrictMode` component. The marked duplicate has a few solution suggestions. – Drew Reese May 18 '22 at 22:17

0 Answers0