Search code examples
reactjswebpackreact-routerbabeljsreact-router-dom

React router No routes matched location "/"


I use bable and webpack for my react app and for routing use react router. I received this error and i have no clue how to fix this beacuse i tried as much as i could: router.js:980 No routes matched location "/" warning @ router.js:980

here is my webpack config

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  mode: "development",
  entry: "./src/index.js",
  output: {
    path: path.join(__dirname, "/dist"),
    filename: "bundle.js",
    publicPath: "/",
    sourceMapFilename: "[name].js.map"
  },
  resolve: {
    extensions: [".jsx", ".js"],
  },
  devtool: "source-map",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [
              [
                "@babel/preset-env",
                {
                  corejs: 3,
                  useBuiltIns: "usage",
                  debug: true,
                  modules: false,
                },
              ],
              "@babel/preset-react",
            ],
          },
        },
      },

      // Loading images
      {
        test: /\.(png|jpg|jpeg|gif|ico)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              outputPath: "images",
              name: "[name]-[sha1:hash:7].[ext]",
            },
          },
        ],
      },

      // Loading fonts
      {
        test: /\.(ttf|otf|eot|woff|woff2)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              outputPath: "fonts",
              name: "[name].[ext]",
            },
          },
        ],
      },

      // Loading CSS
      {
        test: /\.(css)$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: "public/index.html",
    }),
  ],

  devServer: {
    open: true,
    historyApiFallback: true,
  },
};

Here is my index.js

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { BrowserRouter } from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById("root"));

root.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

Here is my App js

import { Route, Routes } from "react-router";
import React from "react";


const Lol = () => {
  return <h1>Lol</h1>
}

const App = () => {
  return (
    <Routes>
      <Route to="/" element={<Lol />} />
    </Routes>
  );
};

export default App;

I searched other solutiions, but I can't find case like i have, I think issue can be in my babel or webpack config


Solution

  • In version 6, the Route component uses path instead of to to specify the URL path to match. so use <Route path="/" element={<Lol />} /> instead of <Route to="/" element={<Lol />} />.