most trivial React Router usecase not working - Route won't render - reactjs

I'm writing my first React app, in Typescript.
Following a tutorial, I've created a single <Link> that references a single <Route> all inside my <Router> component. When I click the Link in my browser, the URL changes, but the Route does not render. I cannot figure out why the Route does not render. I've read a bunch of SO questions, but they're all related to advanced uses of Router; I can't get the most basic usecase to work.
Here's my top-level component:
import React, {Component} from 'react';
import {BrowserRouter as Router, Route, Link} from "react-router-dom";
import {StaticComponent} from "./StaticComponent/StaticComponent";
export default class App extends Component{
render() {
return (
<div className="App">
<Router>
<header className="App-header">
<span>header</span>
<Link to="/staticComponent">Static Component</Link>
</header>
<main>
<span>body</span>
<Route path="/staticComponent" element={StaticComponent}/>
</main>
</Router>
</div>
);
};
}
Here's StaticComponent:
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom'
export function StaticComponent () {
return (
<h2>You are at Static Component</h2>
);
};
As you can see, top-level component displays as you'd expect:
But when I click the link, the StaticComponent does not display:
Is there some small detail I'm getting wrong here? Thanks!

I never figured out how to use the "element" attribute on the <Route> element, nor the "component" attribute that #gbalduzzi mentioned. However, implementing App.tsx in the following way does work:
import React, {Component} from 'react';
import {BrowserRouter as Router, Route, Link} from 'react-router-dom';
import {StaticComponent} from "./StaticComponent/StaticComponent";
export default class App extends Component<{}, {}>{
render() {
return (
<div className="App">
<Router>
<header className="App-header">
<span>header</span>
<Link to="/staticComponent">Static Component</Link>
</header>
<main>
<span>body</span>
<Route path="/staticComponent">
<StaticComponent />
</Route>
</main>
</Router>
</div>
);
};
}
Also, line 2 of StaticComponent.tsx seems to have been unnecessary; I have since taken it out.
Finally, make sure you're using the correct version of React Router. The JSX files that work for React Router 5 do not seem to work for React Router 6.

Related

ReactJS - Load different component when go to new page

Good evening to all. I am new to ReactJS and I do not understand how I can load different content per page and at the same time have some fixed sections such as the header ( after click a LINK ).
I have created three components and I render them to the homepage.When I click the about link I would like to load Header & Learn.js component ONLY. How can I manage that?
Thanks a lot!
index.js
import React from "react";
import ReactDOM from "react-dom";
import Header from './components/header'
import MiddleMan from './components/middleman'
import BottomHero from './components/bottomhero'
import './web.css';
function App(){
return (
<React.Fragment>
<Header />
<MiddleMan />
<BottomHero />
</React.Fragment>
);
}
ReactDOM.render(<App />, document.querySelector(".container"));
middleman.js
import React, { Component } from 'react';
import Learn from './learn';
import {
BrowserRouter as Router,
Routes,
Route,
Link,
} from "react-router-dom";
class MiddleMan extends React.Component {
render() {
return (
<div className="middle JV--row JV--a--center JV--spacer">
<Router>
<ul>
<li><Link to="about">ABOUT ME</Link></li>
<li><Link to="project">PROJECTS</Link></li>
<li><Link to="exp">PR. LANGUAGES / TECHNOLOGIES</Link></li>
</ul>
<Routes>
<Route path="about" element={<Learn/>}></Route>
</Routes>
</Router>
</div>
);
}
}
export default MiddleMan;
learn.js
import React from 'react';
import Header from './header'
class Learn extends React.Component {
render(){
return (
<h2>BBBBBBBBBBBBBBBBBB</h2>
);
}
}
export default Learn;
Your app structure is a little weird considering how react-router-dom is meant to be used. I'd suggest you read their documentation!
Now, trying to give a more specific answer, you should wrap your App component in a BrowserRouter. Your routes could be inside a Switch component and to implement this Header behavior that you wish to achieve I suggest you check the answers to this question.

Embedded react router does not display components

Using react-router-dom, components are not displaying inside my embedded router.
Made sure all of the spellings were correct, the components load fine without the router.
import React, { Component } from 'react'
import { Router, Route, Switch } from 'react-router-dom'
import Constants from '../helpers/Constants'
import Header from "./Header";
import SideBar from "./SideBar";
import Candidate from "./Candidate";
import Settings from "./Settings";
export default class Base extends Component {
render() {
return (
<div>
<Header/>
<SideBar/>
<div id="app-body">
<Switch>
<Route component={Candidate} exact path="/#candidates"/>
<Route component={Settings} exact path="/#settings"/>
</Switch>
</div>
</div>
)}
}
nothing is visible

How to make using of react-router-dom for routing in Reactjs?

I have made 3 components
1)Navbar
2)Content
3)Pagination
On home page I want to display all 3 components.
I have made another component Matchinfo which should get displayed when we click on view stats button (see screenshot for more clarification) .
In app.js how should I make use of Routes so that 3 components will get display on home page i.e localhost:3000/ and when I click on view stats button it should render component Matchinfo.
In app.js :
import React, { Component } from 'react';
import { Route, NavLink, HashRouter } from "react-router-dom";
import './App.css';
import Navbar from './components/navbar';
import Content from './components/content';
import Pagination from './components/pagination';
import Matchinfo from './components/matchinfo';
class App extends Component {
render() {
return (
<div className="App">
<div className="content">
<Route path="/" component={Navbar, Content, Pagination}/>
<Route path="/match" component={Matchinfo}/>
</div>
</div>
);
}
}
export default App;
You no need to call these components in app.js using routes. I would request you to create sepearte component like Home(see example below home.js).
Then, In app.js call Home component
import Home from './components/home';
<Route path="/" component={Home}/>
create home.js under components
Call Navbar, Content annd Pagination components in Home component
import React, {Component} from "react";
import Navbar from './components/navbar';
import Content from './components/content';
import Pagination from './components/pagination';
class Home extends Component {
constructor(props) {
super(props);
this.state = {
}
}
componentWillMount() {
}
render() {
return (
<div>
<Navbar/>
<Content />
<Pagination/>
</div>
)
}
}
export default Home;
Since you want to display Navbar, Content annd Pagination components in home page so do it like above way. Here Home is your parent component and Navbar, Content annd Pagination are child components to Home.
One route is enough mostly for one web page and in React most of times you will play with child components. You no need to configure all the components with routes.
There are several ways achieving the result.
The first one is using render method for home Route. Also, use exact attribute of Route to ensure the location is matched exactly.
import React, { Component } from 'react';
import { Route, NavLink, HashRouter } from "react-router-dom";
import './App.css';
import Navbar from './components/navbar';
import Content from './components/content';
import Pagination from './components/pagination';
import Matchinfo from './components/matchinfo';
class App extends Component {
render() {
return (
<div className="App">
<div className="content">
<Route path="/" render={(props) => (
<React.Fragment>
<Navbar/>
<Content />
<Pagination/>
<React.Fragment/>
)} exact/>
<Route path="/match" component={Matchinfo} exact/>
</div>
</div>
);
}
}
export default App;
The second one, create auxiliary component Home, for example, and include it in your route, like this:
<Route path="/" component={Home} exact/>

React rendering unnecessary component in my app

New to react and working with React Router so that I have many pages.
I am in my Home.jsx and it looks like this.
import React, { Component } from 'react';
import randomimage from '../imagefolder/rentalbackground.jpg';
import Header from './Header';
import Footer from './Footer';
import Rentals from './Rentals';
import {
BrowserRouter as Router,
Route,
Redirect,
Link
} from 'react-router-dom';
class Home extends Component {
render() {
return (
<div>
<Header />
<Router>
<div>
<Link to="/rentals">Rentals</Link>
<main>
<Route path="/" component={Rentals} />
</main>
</div>
</Router>
<p>some paragraph here</p>
<img src={randomimage} alt="imagerand" />
<Footer />
</div>
);
}
}
export default Home;
And my Rentals component looks like this.
import React, { Component } from 'react';
class Rentals extends Component {
render() {
return (
<div>
<p>this is for all the rentals</p>
</div>
)
}
}
export default Rentals;
What I am trying to do is create a page called localhost:3000/rentals which only displays the paragraph from the "Rentals" component in a new page. But when I click on the rentals link, which is on the Home.jsx, it displays all the components from the "Home" component including the picture, and the Header and the Footer components too.
I tried using exact path on the Route and nothing happens. How might I achieve this?
This is because you have placed your Router component inside your Home component which in turn have your Header and Footer. So all child components will be rendered inside your Home component.
Your router component should be on the top level of your App and all other components like Home, Rentals etc should be added as a child to the router.
Just to give you an example, it should be something like this.
//Your app initialisation, Top Level
ReactDOM.render(
<div style={{height: '100%'}}>
//All Your routes can be exported at one place and passed to your router as props. This will also help you maintain routes at one place
<Router history={browserHistory} children={routes}/>
</div>,
document.getElementById('root')
)
Will suggest you to read more about using React router and best practices since this is an architecture problem and quite broad topic to be answered here.

React Router does not update View

I am beginning to work with React and I have an issue understanding how to make react-router work.
In my project, I created an App.js file that exports a simple render function:
export default class App extends React.Component {
render () {
return <Router>
<div>
<Link to="/connected">Login</Link>
<Link to="/">Home</Link>
<Route path="/connected" component={Connected}/>
<Route exact path="/" component={LoginPage}/>
</div>
</Router>
}
}
Of course, on the top of the file, I import two components: Connected and LoginPage and a bunch of other components:
import React from 'react'
import {
HashRouter as Router,
Route,
Link
} from 'react-router-dom'
import LoginPage from './pages/LoginPage'
import Connected from './pages/Connected'
My components are quite simple:
import React from 'react'
export default class LoginPage extends React.Component {
render () {
return <div>
Hello World 1
</div>
}
}
And pretty much the same for the second component.
My problem is:
When I click on a <Link to="/">Home</Link>, the page is not updated with the LoginPage component. But if I refresh the page, the LoginPage components appears. If I click on <Link to="/connected">Login</Link>, the router does not update the page and I also need to refresh completely the page to display the right component.
What am I doing wrong? What other informations would you need to help me resolve my problem?
Copy this into 'Package.json' > 'dependencies': "react-router": "^2.4.0",
Then do npm install
Once done this:
Try this:
First Component (Routes, it is App.jsx) (Maybe you need to change routes of imports)
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, browserHistory, Redirect } from 'react-router';
import LoginPage from './pages/LoginPage'
import Connected from './pages/Connected'
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={LoginPage}/>
<Route path="/connected" component={Connected}/>
</Router>
, document.getElementById('root')
);
Second component (Home):
import React from 'react';
import { Link } from 'react-router';
export default class Home extends React.Component {
render() {
return(
<div>
<h1> This is HomePage </h1>
<h3> Click to go HomePage </h3>
<Link to="/">Home</Link>
<h3> Click to go Connected </h3>
<Link to="/connected">Connected</Link>
</div>
)
}
}
Third component (Connected):
import React from 'react';
import { Link } from 'react-router';
export default class Connected extends React.Component {
render() {
return(
<div>
<h1> This is ConnectedPage </h1>
<h3> Click to go HomePage </h3>
<Link to="/">Home</Link>
<h3> Click to go Connected </h3>
<Link to="/connected">Connected</Link>
</div>
)
}
}

Resources