I'm migrating to ReactRouter v4 and it's just been proving a hassle to me. I have a file in app.js in which i'm trying to setup a simple routing system.
import React, { Component } from 'react';
import { Router, Route, IndexRoute, hashHistory, Redirect } from 'react-router';
import LoginPage from './LoginPage';
import NavigationBar from './NavigationBar';
class App extends Component {
render() {
return (
<Router history={history}>
<Route exact path='/' component={NavigationBar}></Route>
<Route exact path='/login' component={LoginPage}></Route>
</Router>
);
}
}
The code compiles perfectly well, but for some reason on the browser, the navigation bar is shown instead of the login page when the url is localhost:3000/login
The image of the nagivation bar showing instead of the login
I intended the login to be shown at /login and the navigation bar to be shown at /, but instead the navigation bar is shown at both /login and /.
I've been following the examples on the react-router training guide here, and I can't seem to figure out what I'm doing wrong.
Maybe it's just a typo in your post here, but you're extending Compoent instead of Component. I would guess you'd see worse issues than getting the wrong component if that was happening in your code.
I prefer BrowserRouter (I don't have to support legacy browsers), but I got your code working like this:
import React, { Component } from 'react';
import { Route } from 'react-router';
import { HashRouter as Router } from 'react-router-dom';
import LoginPage from './LoginPage';
import NavigationBar from './NavigationBar';
class App extends Component {
render() {
return (
<Router>
<div>
<Route exact path='/' component={NavigationBar}></Route>
<Route exact path='/login' component={LoginPage}></Route>
</div>
</Router>
);
}
}
You'll have to install react-router-dom for this code to run.
Visiting http://localhost:3000/#/login, I get the Login component. If you change HashRouter to BrowserRouter, you can see your component render at http://localhost:3000/login.
Related
I am trying to make a multi page app with react routing.
I am have some questions as to how I should structure the routing in the react project.
I want to load my component in my app.js file. In my Home component I would like to have the ability to press a button which will take me to the Poems component, I want the code to be clean and structured into components, therefore I dont want to do all this in the app.js file.
If someone would explain to me how to best do this I can from there be able to route around to multiple pages afterwards depending on the page you are on. I dont want to have a global menu currently (I will want that in the Poems component later though).
Here is my current App.js file & Home.jsx component code just for a more easily adjustable experience for you guys!
Currently it is not optimized to work so if anyone knows a good routing solution for my issue, please give me an example of the routing fix.
Thanks alot
/Jacob
import React from 'react'
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom'
import './App.scss'
import { Home, Poems, Favourites } from './Pages'
const App = () => {
return (
<Router>
<div className="app">
<Home />
<Routes> {/* I read that the Switch was replaces with Routes */}
<Route path="/" exact component={ Home } />
<Route path="/Poems" component={ Poems } />
<Route path="/Favourites" component={ Favourites } />
</Routes>
</div>
</Router>
)
}
export default App
import React from 'react'
import { Route, BrowserRouter as Router, Routes, Link } from 'react-router-dom'
import { Poems } from './Pages'
import './Home.scss'
const Home = () => {
return (
<Router>
<div>
<h1>Petry For All</h1>
<Routes>
<Route path="/Poems" component={ Poems } />
<Link to="/Poems">Poems</Link>
</Routes>
</div>
</Router>
)
}
export default Home
You don't need to (and actually shouldn't) duplicate the <Router> component in all of the route pages. It is only the root component that is acting as a router. So you can keep the App component the same, and then replace the Home component with the following:
import React from 'react'
import { Poems } from './Pages'
import './Home.scss'
const Home = () => {
return (
<div>
<h1>Petry For All</h1>
<Link to="/Poems">Poems</Link>
</div>
)
}
export default Home
The <Link> component resolves into an anchor element which, when clicked, navigates the user to the route passed into the to property.
React router switch works fine when I using Redirects, Navlinks or other library staff.
The problem is in function loadOrderPage. When its fired, window location changes OK but page not re-render accordingly to react-router switch rules, so component for '/order' doesn't render after this function complete.
here is my simple react class
Live working example here: https://codesandbox.io/s/delicate-wildflower-sdvd4?file=/src/App.js
import React from "react";
import {
BrowserRouter as Router,
Redirect,
Route,
Switch,
} from "react-router-dom";
import Startpage from "./components/startpage";
import Orderpage from "./components/orderpage";
import { withRouter } from "react-router";
class App extends React.Component {
.......
loadOrderPage = () => {
this.props.history.push("/order");
};
render() {
return (
<Router>
<Switch>
<Route path="/order">
<Orderpage />
</Route>
<Route path="/">
<Startpage/>
</Route>
</Switch>
</Router>
);
}
}
export default withRouter(App);
Whats wrong? I want to able change window location in functions and waiting that react router would render correct components under switch rules. May be its more suitable methods are available?
You cannot have two defined, both in your index.js and in your App.js. Remove the <Router></Router> from your App.js.
Make sure to add exact to your <Route />.
<Route exact path="/order">
<Orderpage />
</Route>
<Route exact path="/">
<Startpage/>
</Route>
I can't get my Router working in react. I am using webpack, thinking I miss some code in webpacn.config.js.
I tried to retype my url to localhost:8080/signin or localhost:8080/signup.
I got en error of
Cannot GET /signin
import React from 'react'
import { BrowserRouter, Switch, Route } from 'react-router-dom'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'materialize-css/dist/css/materialize.min.css'
import Navbar from './Components/layout//Navbar'
import SignIn from './Components/authentication/SignIn'
import SignUp from './Components/authentication/SignUp'
class App extends React.Component {
constructor() {
super()
}
render() {
return (
<BrowserRouter>
<div>
<Navbar />
<Switch>
<Route exact path='/signin' component={SignIn} />
<Route exact path='/signup' component={SignUp} />
</Switch>
</div>
</BrowserRouter>
)
}
}
export default App
I think it's not related to Webpack. react's router system handles the routes in client-side so if you want to type URL manually, you should create a fallback URL to root path in the back-end side (based on your back-end tech) then react can handle entered URL.
This is my first crack at trying to integrate React-Router4 into a React/Redux app. I've been stuck on the first hurdle for a couple hours as I'm reading the docs and a (very good) CSS-Tricks article.
The issue is that the won't render the 'Whatever' value I input at the component prop, which should be a component.
I've actually read almost entirely thru the (excellent) docs, beginning here: https://reacttraining.com/react-router/web/guides/quick-start
And I've consulted this article: https://css-tricks.com/react-router-4/
Which also includes this Codepen (to see if I did something obvious/stupid): https://codepen.io/bradwestfall/project/editor/XWNWge?preview_height=50&open_file=src/app.js
I'm just hoping someone on SO might be able to nudge me over this hump. I was expecting to at least be able to render this very simply without any additional libraries or adjustment to my stores based on: https://reacttraining.com/react-router/web/guides/redux-integration
entry point, index.js:
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import store from './stores'
import { Provider } from 'react-redux'
import Home from './components/Home'
import TopStories from './components/TopStories'
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
// app entry point
const app = (
<Provider store={store.configure(null)}>
<Router>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/topstories" component={TopStories} />
</Switch>
</Router>
</Provider>
)
// virtual DOM render
ReactDOM.render(app, document.getElementById('root'))
TopStories.js component (renders ok in the '/' path if I switch it w/ Home)
import React, { Component } from 'react'
export default (props) => {
return(
<div>Top Stories component </div>
)
}
I was attempting to test by typing the path into the browswer.... Duh. I just needed to set up a quick link component and everything worked fine.
I am playing with react router. I have created a simple app using it.
https://react-04-router.herokuapp.com/
If one click on aboutus or contactus, url is changing and new content is being shown. Everything is working perfectly fine till this point.
But if I refresh on the new link https://react-04-router.herokuapp.com/aboutus. it shows error page as "Not Found".
I have not configured /aboutus route at the server side and I guess this is the reason "Not found" error is coming but this makes me wonder following
If client side routing can not support on refresh, wouldn't it not impact user experience ?
To avoid impact on user experience if I decide to go with server side, I know i would lose faster response of client side routing, other than this, is there anything that I might miss out if I do not use client side routing.
Here is my router file code:
import React, { Component } from 'react'
import { Switch, Route } from 'react-router-dom'
import AboutUs from '../presentation/AboutUs';
import ContactUs from '../presentation/ContactUs';
import Home from '../presentation/Home';
import Navbar from '../presentation/Navbar';
class MainLayout extends Component {
render(){
return(
<div >
<Navbar/>
<Switch>
<Route exact path='/' component={Home}/>
<Route path='/aboutus' component={AboutUs}/>
<Route path='/contactus' component={ContactUs}/>
</Switch>
</div>
)
}
}
export default MainLayout
And here is app.js file which renders
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter } from 'react-router-dom'
import Header from './presentation/Header';
import Footer from './presentation/Footer';
import MainLayout from './layout/MainLayout'
class App extends Component {
render(){
return(
<div className="container-fluid">
<Header />
<MainLayout/>
<Footer />
</div>
)
}
}
ReactDOM.render(
<BrowserRouter>
<App/>
</BrowserRouter>,
document.getElementById('root'))
I would suggest using HashRouter in that case, see Hash Router here