DEV Community

GihanRangana
GihanRangana

Posted on

5 2

Routes not working when react app is build

I created react app react router 4.but I build my project using npm run build route URL not working and display 404 page.how can I fix that? I have no idea.this is my code

import React from "react";
import ReactDOM from "react-dom";
import registerServiceWorker from "./registerServiceWorker";
import { BrowserRouter as Router,Switch,Route } from "react-router-dom";

import { createMuiTheme, MuiThemeProvider } from "@material-ui/core/styles";

import "react-datepicker/dist/react-datepicker-cssmodules.css";
import "./styles/main.scss";


import App from "../App";
import AdminHome from "../pages/admin/home";
import Login from "../pages/login";
import PrivateRoute from "./privateRoute";

const theme = createMuiTheme({
    palette: {
        type: "dark"
    }
});

ReactDOM.render((

Not Found

}/> ), document.getElementById("root")); registerServiceWorker();

Can anyone help me to fix this?

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (5)

Collapse
 
matteusbarbosa profile image
Matteus BS

This is an apache-related issue

Make sure your Apache server redirects every request to index.html file. write the following code to .htaccess

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

Collapse
 
adan199 profile image
M Adan Sarfraz

i just sign there up to say thank you for your answer
you saved me from my horrible client

Collapse
 
kram3r91 profile image
kram3r91 • Edited

For me works! i don't have any htaccess rules before

Collapse
 
tylerferrara profile image
Tyler Ferrara

At this time, you are telling react to render "Not Found" with an additional "}/>" that doesn't seem to belong.

Follow along with the React Router Docs: reacttraining.com/react-router/web...

// First import the correct modules
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

/*
Your Code Here
*/

// Render your root Component to the DOM
ReactDOM.render((
// Wrapping Routes in Router
// Render Home Component
// Render Blog Component
// Only renders when no match was found

), document.getElementById("root"));

registerServiceWorker();

Use this as a template for the routing, but make sure to checkout the original documentation. They lay it out clean and simple.

Hope this helps :D

Collapse
 
gihanrangana profile image
GihanRangana

thanks for helping.routing component is not shown on the post

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay