@Markus2024

Как преобразовать следующий код React Router Dom версии 6.9.0 в V6.26.2?

Я хочу преобразовать следующий код в react-router-dom версию 6.26.2. Сейчас в консоль выезжает ошибка: "Uncaught Error: [div] is not a Route component. All component children of Routes must be a Route or React.Fragment".

Код:

import { React } from "react";
import { Routes, Route } from "react-router-dom";
import { Cart } from "./components/cart/Cart";
import { Catalog } from "./components/catalog/Catalog";
import { About } from "./pages/About";
import { MainPage } from "./components/mainPage/MainPage";
import { Page404 } from "./pages/Page404";
import { ProductPage } from "./components/productPage/ProductPage";
import { Menu } from "./components/menu/Menu";
import { Contacts } from "./pages/Contacts";
import { Footer } from "./UI/Footer";
import { Banner } from "./UI/Banner.jsx";
import { useDispatch } from "react-redux";
import { resetClicks } from "./app/store/searchBarSlice";

function App() {
    const dispatch = useDispatch();

    const hideMenuSearchBar = () => {
        dispatch(resetClicks());
    };

    return (
        <Routes basename="/test">
            <div className="App">
                <Menu />
                <main className="container" onClick={hideMenuSearchBar}>
                    <div className="row">
                        <div className="col">
                            <Banner />
                            <Routes>
                                <Route
                                    path="/"
                                    exact
                                    element={<MainPage />}
                                ></Route>

                                <Route
                                    path="/about"
                                    element={<About />}
                                ></Route>
                                <Route
                                    path="/products/:prdId"
                                    element={<ProductPage />}
                                ></Route>
                                <Route path="/cart" element={<Cart />}></Route>
                                <Route
                                    path="/contacts"
                                    element={<Contacts />}
                                ></Route>
                                <Route path="*" element={<Page404 />}></Route>
                                <Route
                                    path="/:catId"
                                    element={<MainPage />}
                                ></Route>
                                <Route
                                    path="/catalog/:catId?"
                                    element={<Catalog isCatalogPage={true} />}
                                ></Route>
                            </Routes>
                        </div>
                    </div>
                </main>

                <Footer />
            </div>
        </Routes>
    );
}

export default App;
  • Вопрос задан
  • 71 просмотр
Решения вопроса 1
grantur5707
@grantur5707
Full Stack Web Developer
import { React } from "react";
import { Routes, Route } from "react-router-dom";
import { Cart } from "./components/cart/Cart";
import { Catalog } from "./components/catalog/Catalog";
import { About } from "./pages/About";
import { MainPage } from "./components/mainPage/MainPage";
import { Page404 } from "./pages/Page404";
import { ProductPage } from "./components/productPage/ProductPage";
import { Menu } from "./components/menu/Menu";
import { Contacts } from "./pages/Contacts";
import { Footer } from "./UI/Footer";
import { Banner } from "./UI/Banner.jsx";
import { useDispatch } from "react-redux";
import { resetClicks } from "./app/store/searchBarSlice";

function App() {
    const dispatch = useDispatch();

    const hideMenuSearchBar = () => {
        dispatch(resetClicks());
    };

    return (
        <div className="App">
            <Menu />
            <main className="container" onClick={hideMenuSearchBar}>
                <div className="row">
                    <div className="col">
                        <Banner />
                        <Routes basename="/test">
                            <Route path="/" exact element={<MainPage />} />
                            <Route path="/about" element={<About />} />
                            <Route path="/products/:prdId" element={<ProductPage />} />
                            <Route path="/cart" element={<Cart />} />
                            <Route path="/contacts" element={<Contacts />} />
                            <Route path="*" element={<Page404 />} />
                            <Route path="/:catId" element={<MainPage />} />
                            <Route path="/catalog/:catId?" element={<Catalog isCatalogPage={true} />} />
                        </Routes>
                    </div>
                </div>
            </main>
            <Footer />
        </div>
    );
}

export default App;
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы