getting 404 error in react-router-dom - reactjs

I have started with react-router-dom. my main page rendered successfully but when I am clicking on hyper provided by Link component it is not redirecting but if I hit url manually it is giving me 404 error. please help me in this?
here is my config file
import React from 'react'
import { render } from 'react-dom'
import { createStore, applyMiddleware,compose } from 'redux'
import { Provider } from 'react-redux'
//import rootReducer from './reducers'
import createLogger from 'redux-logger'
import thunk from 'redux-thunk'
import {BrowserRouter} from 'react-router-dom'
import promise from "redux-promise-middleware"
import logger from "redux-logger"
import {fetchUsers} from "./action/UserAction"
import {fetchChart} from "./action/ChartAction"
import {fetchNames} from "./action/SearchAction"
import reducer from "./reducers"
import routes from "./routes"
const middleware = applyMiddleware(promise(), thunk, logger())
const store= createStore(reducer,middleware)
store.dispatch(fetchUsers());
store.dispatch(fetchChart());
render(
<Provider store={store}>
<BrowserRouter >
{routes}
</BrowserRouter>
</Provider>,
document.getElementById('root')
)
my routes file
import App from "./pages/App"
import Users from "./pages/Users"
import Charts from "./pages/Charts"
import React from "react"
import { Route } from "react-router-dom"
export default (
<switch>
<Route path="/" component={App}/>
<Route path="/users" component={Users}/>
<Route path="/charts" name="charts" component={Charts}/>
</switch>
);
App.js
import {Link} from "react-router-dom"
import React from "react"
const App = () =>(
<div>
<h1> this is main page </h1>
<Link to="charts">charts</Link>
</div>
)
export default App
Users.js
import React from "react"
const User = () =>(
<div>
<h1> this is user </h1>
</div>
)
export default User
Charts.js
import React from "react"
const Chart = () => (
<h1> this is chart </h1>
)
export default Chart
UPDATE 1:
I do not know what is going on. but I alter the sequence in routes.js it is working by clicking on url. but still if I hit the url manually or refresh there it is still not working. below is my updated routes.js
import App from "./pages/App"
import Users from "./pages/Users"
import Charts from "./pages/Charts"
import React from "react"
import { Route,Switch } from "react-router-dom"
export default (
<Switch>
<Route path="/charts" component={Charts}/>
<Route path="/users" component={Users}/>
<Route path="/" component={App}/>
</Switch>
);
UPDATE 2:
if I am using exact path then sequence does not matter. I think I didn't understand use of exact path correctly. but still while hitting url manually or refreshing is not working for me
my final routes.js
import App from "./pages/App"
import Users from "./pages/Users"
import Charts from "./pages/Charts"
import React from "react"
import { Route,Switch } from "react-router-dom"
export default (
<Switch>
<Route exact path="/" component={App}/>
<Route exact path="/charts" component={Charts}/>
<Route exact path="/users" component={Users}/>
</Switch>
);

After struggling I got a SO post saying that if my data is not mapping from backend I should use HashRouter. So I changed my browser so I changed my code to this. but I don't know why browser router didn't work
<Provider store={store}>
<HashRouter >
{routes}
</HashRouter>
</Provider>,
document.getElementById('root')

If you are using webpack add this
devServer: {
historyApiFallback: true,
}
when using BrowserRouter "historyApiFallback" should be true

Related

my codes in react don't show up in browser

It worked well yesterday, but today localhost:3000 doesn't show me anything in browser all of sudden.
App.js
import React, { useEffect }from 'react'
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import {connect} from 'react-redux'
import * as auth_actions from './store/actions/auth_action'
import Home from './Authentication/Home'
import Login from './Authentication/Login'
import Signup from './Authentication/Signup'
import Loading from './Authentication/Loading'
import Activate from './Authentication/Activate'
import PasswordReset from './Authentication/PasswordReset'
import PasswordResetConfirm from './Authentication/PasswordResetConfirm'
import Cards from './Swipe/Cards'
import Match from './Swipe/Match'
import Setting from './Setting/Setting'
import Editor from './Setting/Editor'
import Chat from './Chat/Chat'
import ChatPanels from './Chat/ChatPanels'
const App = (props) => {
useEffect(() => {
props.checkAuthenticated();
})
return(
<Router>
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/login' component={Login} />
<Route exact path='/signup' component={Signup} />
<Route exact path='/reset_password' component={PasswordReset} />
<Route exact path='/password/reset/confirm/:uid/:token' component={PasswordResetConfirm} />
<Route exact path='/loading' component={Loading}/>
<Route exact path='/activate/:uid/:token' component={Activate} />
<Route exact path="/chat/:friend/:ChatID" component={Chat}/>
<Route exact path='/swipe' component={Cards}/>
<Route exact path='/setting' component={Setting}/>
<Route exact path='/chatpanel' component={ChatPanels}/>
<Route exact path='/edit' component={Editor}/>
<Route exact path='/match' component={Match}/>
</Switch>
</Router>
);
}
const mapDispatchToProps = dispatch => {
return {
checkAuthenticated: () => dispatch(auth_actions.checkAuthenticated)
}
}
export default connect(null, mapDispatchToProps)(App)
index.js
import ReactDOM from 'react-dom';
import React from 'react'
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import 'antd/dist/antd.css';
import reducer from './store/reducers/index';
import App from './App';
import { composeWithDevTools } from "redux-devtools-extension";
import thunk from 'redux-thunk';
const initialState = {}
const middelware = [thunk]
const store = createStore(
reducer,
initialState,
composeWithDevTools(applyMiddleware(...middelware))
)
const app =
<Provider store={store}>
<App />
</Provider>
ReactDOM.render(app, document.getElementById("root"));
index.html
.
.
.
<title>Speak Up</title>
</head>
<body>
<div id="root" style="width:100%"></div>
<script src="bundle.js" type="text/javascript"></script>
.
.
.
</body>
when I save the code, no errors comes up. In browser, no error messages appears. Before this happened, there were error messages that I expected, and I tried to fix them, but when I debug, It came up. Are there some reasons why this happened?
Could you tell me how to fix it?
Thank you:)
It worked when I changed the port from 3000 to 4000.
.env
PORT = 4000
If you are working on multiple projects or port 3000 is being used then this can cause an issue..happens to me alot!

React router changes url but not page loading

// this is my index.js
import React, { } from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import store from "./store/configureStore";
import { Router } from "react-router";
import "./index.css";
import App from "./App";
import history from "./history";
// append app to dom
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<App />
</Router>
</Provider>,
document.getElementById("root")
);
//this is my app.js
import React, { Component } from 'react';
import { Route, Switch, BrowserRouter } from 'react-router-dom';
import Inscription from "../src/pages/inscription";
class App extends Component {
render() {
return (
< BrowserRouter>
<Route exact path="/" component={Inscription} />
</ BrowserRouter>
);
}
}
export default (App);
I am having changing the view in react with routing. It seems I have tried everything I can google with no luck. the page affiche but doesn't load
Please same one can help mee :(
Try removing the BrowserRouter from the App component. You have already provided a Router in index.js. Also in case you want to define multiple routes, you have to use switch
<Switch>
<Route exact path="/" component={Inscription} />
<Route exact path="/hello" component={HelloComponent} />
<Switch>

React-Redux How to handle slash (/) in production. Redirection from root

I have a build package from React, once I deploy it to the server with name admin, I can not get to that folder with www.home.com/admin, so to get to my project I need to append / and once I write www.home.com/admin/ my project executes.
I changed in package.json "homepage": "." to make relative path and I use HashRouter
How I can handle this, because every time I need to add / to the path in browser
import React, { Component } from 'react';
import './App.css';
import {Route, Switch, HashRouter as Router} from 'react-router-dom'
import {store ,persistor} from './components/store/index';
import { PersistGate } from 'redux-persist/integration/react';
import { Provider } from 'react-redux';
import Display from './components/displayMainScreen/displayMainScreen'
import SignIn from './components/login/indexLogin'
import Logs from './components/logs/logs';
class App extends Component {
render() {
return (
<div className="App">
<Provider store={store}>
<PersistGate persistor={persistor}>
<Router>
<Switch>
<Route exact path='/' component = {SignIn} />
<Route path='/login' component = {SignIn} />
<Route path='/display' component = {Display} />
<Route path='/logs' component = {Logs} />
</Switch>
</Router>
</PersistGate>
</Provider>
</div>
);
}
}
export default App;

routing not happening in react router version 4

I may have done silly mistake this time but I am but getting it what. I started with react router v4 but my routing is not happening. I try to hit url manually as well as by button click no result. here's my route config. and FYI I am using LinkContaier to redirection
import ReactDOM from 'react-dom';
import React from 'react';
import { Provider } from 'react-redux';
import configureStore from './stores/configureStores';
import {BrowserRouter,Route,Switch} from 'react-router-dom'
import HeaderContainer from "./containers/HeaderContainer"
import ProgramProfileContainer from "./containers/ProgramProfileContainer"
const store = configureStore();
ReactDOM.render(
<Provider store={store}>
<BrowserRouter >
<Switch>
<HeaderContainer/>
{/* <Route exact path="/" component={HeaderContainer}/> */}
<Route path="program-profile/:program_id" component={ProgramProfileContainer}/>
</Switch>
</BrowserRouter>
</Provider>, document.getElementById('root')
);
this is my container
import React from "react"
import { connect } from 'react-redux';
export default class ProgramProfileContainer extends React.Component{
render(){
console.log("program profile")
return(
<h1> this is profile </h1>
)
}
}
i hit the url like program-profile/3 but rendered nothing no error in console also
Don't use switch inside browser router :
ReactDOM.render(
<Provider store={store}>
<BrowserRouter >
<div>
<Route exact path="/" component={HeaderContainer}/>
<Route path="/program-profile/:program_id" component={ProgramProfileContainer}/>
</div>
</BrowserRouter>
</Provider>, document.getElementById('root')
);
Your answer seems to be ok. But this is how I do it normally.
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import promise from 'redux-promise';
import reducers from './reducers';
import ProgramProfileContainer from "./containers/ProgramProfileContainer"
const store = configureStore();
ReactDOM.render(
<Provider store={store}>
<BrowserRouter>
<div>
<Switch>
<Route path="program-profile/:program_id" component={ProgramProfileContainer} />
<Route path="/" component={IndexPage} />
</Switch>
</div>
</BrowserRouter>
</Provider>
, document.querySelector('.container'));
When you use Link it should be like this.
<Link to={`/posts/${post.id}`}>
{post.title}
</Link>
If you have any error messages in console, please post them so that we can help you further. Hope this helps. Happy coding.

react-route display blank page

My problem is react-route display blank page and no error display in console or webpack. I try to delete all Router and replace by App but it show me blank page agian.
This is my main.js file.
import React from 'react'
import ReactDOM from 'react-dom'
import {Provider} from 'react-redux'
import {createStore, combineReducers} from 'redux'
import { Router, Route, browserHistory, IndexRoute } from 'react-router'
import Index from './containers/Index.js'
import App from './containers/App.js'
import pollReducer from './reducers/pollReducer.js'
var reducers = combineReducers({
poll: pollReducer
})
var store = createStore(reducers);
ReactDOM.render(
<Provider store={store}>
<Router>
<Route path="/" component={Index} />
<Route path="/question" component={App} />
</Router>
</Provider>
, document.getElementById('app'))
and this is my App and Index js file
import React from 'react'
class App extends React.Component {
render(){
return(
<div>
This is App page
</div>
)
}
}
export default App
This is my Index.js file
import React from 'react'
class Index extends React.Component {
render(){
return(
<div>
This is asdasdasda
</div>
)
}
}
export default Index
Thank you for help.
Because you forgot to define the history, Use this:
<Router history={browserHistory}>
<Route path="/" component={Index} />
<Route path="/question" component={App} />
</Router>

Resources