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.
Related
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.
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
I'm trying to add routes to my react project. I have three separate components:
Window.js
import React, { Component } from 'react';
import SideBar from "../SideBar/SideBar";
import MainBody from "../MainBody/MainBody";
import { BrowserRouter as Router} from "react-router-dom";
class Window extends Component{
render() {
return (
<div>
<Router>
<SideBar />
<MainBody />
</Router>
</div>
);
}
}
export default Window
SideBar.js
import React, { Component } from 'react';
import PropTypes from "prop-types";
import { BrowserRouter as Link} from "react-router-dom";
class SideBar extends Component {
render() {
return(
<Link to="/">Home</Link>
<Link to="/about">About</Link>
);
}
}
export default SideBar;
MainBody.js
import React, { Component } from 'react';
import Home from "./Home/Home";
import About from "./About/About";
import { BrowserRouter as Route} from "react-router-dom";
class MainBody extends component {
render() {
return(
<div>
<Route exact path="/" component={Home}>
<Route path="/about" component={About}>
</div>
);
}
}
export default MainBody;
So basically, when I click one of my links in SideBar, I want to transition to that link in my Main Body (the Home and About just display their titles). However, When I run this, my Window, MainBody, and SideBar components work but my Home and About components do not get displayed. I've properly imported the router components into each component file. If I place the Routes from MainBody into the Window component, they get displayed (Not sure if the router links work with it though). Any suggestions would be helpful!
There are few typos in that snippets, dunno ifit was made when rewriting or u have it in your codebase, but be careful about them (component instead of Component, wrongly writtern render function with multiple elements returned etc).
import { BrowserRouter as Link} from "react-router-dom";
Theese imports are also wrong, You have to import Link not something as Link. You are only renaming import BrowserRouter to Link.
import { Link } from "react-router-dom";
And same for Route.
Here is example codesanbox . Let me know if that is what you wanted.
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.
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>
)
}
}