Home > Enterprise >  Product components not showing anymore after i started configuring routes, any idea?
Product components not showing anymore after i started configuring routes, any idea?

Time:11-13

I got errors on the console saying i should wrap my child route with Routes and all of routes still wrapped with Container, even when i did that the error still persists and products aren't showing yet.. And if remove Routes again nothing will show even the navbar. Also i get this warning "Matched leaf route at location "/" does not have an element. This means it will render an with a null value by default resulting in an "empty" page."

App.js:

import "./App.css";
import { Container } from "react-bootstrap";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Footer from "./components/Footer";
import Header from "./components/Header";
import HomeScreen from "./components/screens/HomeScreen";
import ProductScreen from "./components/screens/ProductScreen";

function App() {
  return (
    <Router>
      <Header />
      <main className="my-3">
        <Container>
          <Routes>
            <Route path="/" component={HomeScreen} exact />
            <Route path="/product/:id" component={ProductScreen} exact />
          </Routes>
        </Container>
      </main>
      <Footer />
    </Router>
  );
}

export default App;

HomeScreen.js:

import React from "react";
import products from "../../products";
import { Row, Col } from "react-bootstrap";
import Product from "../Product";

function HomeScreen() {
  return (
    <div>
      <h1 className="text-center">Latest Products</h1>
      <Row>
        {products.map((product) => (
          <Col key={product.id} sm={12} md={6} lg={4} xl={3}>
            {/* <h3>{product.name}</h3> */}
            <Product product={product} />
          </Col>
        ))}
      </Row>
    </div>
  );
}

export default HomeScreen;

Product.js:

import React from "react";
import { Card } from "react-bootstrap";
import Rating from "./Rating";
import { Link } from "react-router-dom";

function Product({ product }) {
  return (
    <Card className="my-3 p-3 rounded">
      <Link to={`/product/${product.id}`}>
        <Card.Img src={product.image} />
      </Link>

      <Card.Body>
        <Link to={`/product/${product.id}`}>
          <Card.Title as="div">
            <strong>{product.name}</strong>
          </Card.Title>
        </Link>
        <Card.Text as="div">
          <div className="my-3">
            {product.rating} from {product.numReviews} reviews
          </div>
        </Card.Text>
        <Card.Text as="h3">$ {product.price}</Card.Text>
        <Rating
          value={product.rating}
          text={`${product.numReviews} reviews`}
          color={"#f8e825"}
        />
      </Card.Body>
    </Card>
  );
}

export default Product;

CodePudding user response:

I think you have mistaken react-router-dom v6 version with v5. You are using Routes that is available only with v6 version together with Route with component prop, that is available only with v5 version. I believe you are using v6 version.

If so, just change:

<Route path="/" component={HomeScreen} exact />

to

<Route path="/" element={<HomeScreen />} />

Note: exact prop has been removed from v6 version as well.

  • Related