I'm coming back to you after a problem I'm having with redirects in React, I've decided to resume my project from 0 and I'm still having the same problem..
When I request this page http://localhost:3000/profil I am automatically redirected to this same page with a slash at the end of the url, e.g. http://localhost:3000/profil/.
Why does the / appear automatically at the end of the url http://localhost:3000/profil/? I'm not asking anywhere.
For the test, the Profil page and the Home page are exactly the same and should return the same image, except that only the Home page returns the image.
Here is the code I am using
src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './styles/index.scss'
ReactDOM.render(
<App />,
document.getElementById('root')
);
src/App.js
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import Routes from './components/Routes';
const App = () => {
return (
<div>
<BrowserRouter>
<Routes />
</BrowserRouter>
</div>
)
};
export default App;
src/components/Routes/index.js
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Home from '../../pages/Home';
import Panier from '../../pages/Panier';
import Profil from '../../pages/Profil';
const index = () => {
return (
<Routes>
<Route path='/' element={<Home />} />
<Route path='/profil' element={<Profil />} />
<Route path='/mes-paniers' element={<Panier />} />
<Route path='*' element={<Home />} />
</Routes>
)
};
export default index;
src/pages/Profil
import React from 'react';
const Profil = () => {
return (
<div className="profil-page">
<div className="log-container">
<div className="img-container">
<img src="./img/log.svg" alt="pic-profil" />
</div>
</div>
</div>
)
};
export default Profil;
The Home page looks like the Profil page,
src/pages/Home
import React from 'react';
const Home = () => {
return (
<div className="profil-page">
<div className="log-container">
<div className="img-container">
<img src="./img/log.svg" alt="pic-profil" />
</div>
</div>
</div>
);
};
export default Home;
The image is not found on "/profil/" while on "/home" it is displayed.
I don't understand where the problem comes from.
The problem seems solved to me. I had to reset the default settings of Google Chrome. I can now load my page /profil and no longer /profil/!
EDIT :
It still doesn't work on Brave browser. Can't get just the url http://localhost:3000/profil . It always sends me /profil/.
Related
I have a menu with a "login" link to open to the login page. When I click on the "login" link it does nothing...doesn't open page, just empty click. My expectation was that the login page would be displayed. I am using an existing html template that uses UL lists for the menu. I'd like to keep using the UL list for the menu, if possible, as it fits in the existing CSS and layout of the page nicely.
I've researched react-router-dom, links, routes, etc for the last 3 hours...I can't find a solution for this, I see how others made it work somehow for their project, but when I try the solutions in my project it doesn't work for me...perhaps I've got something out of order? I'm new to react, but have read/watched a bunch of tutorials and would welcome any advice or guidance.
The problem appears to be inside "HeaderMenu.js". I've included the details of the components. thanks in advance.
The rendered anchor tag looks like this after React renders it:
Login
Here is how the project is set up:
Folder Structure
/src/Index.js
/src/App.js
/src/components/Home.js
/src/components/header.js
/src/components/HeaderMenu.js
/src/components/Login.js
Index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { BrowserRouter, Routes, Route } from "react-router-dom";
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
App.js
import './App.css';
import Home from "./components/Home";
function App() {
return (
<>
<Home />
</>
);
}
export default App;
Home.js
import Header from "./header";
import Body from './body';
import Footer from "./footer";
const Home = () => {
return (
<div className="page-container">
<Header />
<Body />
<Footer />
</div>
);
}
export default Home;
header.js
import React from 'react';
import HeaderMenu from "./HeaderMenu";
const Header = () => {
return <>
<header className="header-area header-sticky wow slideInDown" data-wow-duration="0.75s" data-wow-delay="0s">
<div className="container">
<div className="row">
<div className="col-12">
<nav className="main-nav">
<HeaderMenu />
</nav>
</div>
</div>
</div>
</header>
</>
}
HeaderMenu.js
import React from "react";
import { Routes, Route, Link } from "react-router-dom";
import Login from './Login';
const HeaderMenu = () => {
return (
<>
<ul className="nav">
<li className="scroll-to-section"><a href="#top" className="active" >Home</a></li>
<li className="scroll-to-section">About Us</li>
<li className="scroll-to-section">Services</li>
<li className=""><Link to={"/Login"}>Login</Link></li>
<li className="scroll-to-section"><div className="main-red-button">Contact Now</div></li>
</ul>
</>
)
}
export default HeaderMenu;
Login.js
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
const Login = () => {
return (
<div>
<h1> Login Page </h1>
</div>
);
};
export default Login;
You have to define routes. Like on which route, which component has to be render. For example: for "/login" you want to define a route in "app.js" file. Like below:
import "./App.css";
import Home from "./components/Home";
import { BrowserRouter, Routes, Route } from "react-router-dom";
// import all page components like following
// import Login from "./components/Login";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />} />
</Routes>
</BrowserRouter>
);
}
export default App;
There is a problem using routes in KnowledgeBase.js (code below). I'm trying to write them "inside" another Route (which is in App.js), but nothing works. Perhaps someone can find the problem.
p.s. the console does not give any errors
App.js
import React from "react";
import './App.css';
import Header from "./Components/Header/Header";
import Main from "./Components/Main/Main";
import {Route, Routes} from "react-router-dom";
import Home from "./Components/Main/Home/Home";
import Nav from "./Components/Nav/Nav";
import KnowledgeBase from "./Components/Main/KnowlegeBase/KnowledgeBase";
import Messages from "./Components/Main/Messages/Messages";
function App(props) {
return (
<div className="App">
<Header forHeader={props.state}/>
<Nav forNav={props.state}/>
<Main forMain={props.state}>
<Routes>
<Route path='/home' element={<Home forHome={props.state}/>}/>
<Route path='social-network/' element={<Home forHome={props.state}/>}/>
<Route path='/knowledge-base/*' element={<KnowledgeBase forKnowledgeBase={props.state}/>}/>
<Route path={"/messages"} element={<Messages forMessages={props.state}/>}/>
</Routes>
</Main>
</div>
);
}
export default App;
KnowledgeBase.js
import React from "react";
import style from './KnowledgeBase.module.css';
import Menu from "./Menu/Menu";
import Blocks3 from "./Blocks3/Blocks3";
import {Route, Routes} from "react-router-dom";
import BaseAll from "./BaseAll/BaseAll";
import Blocks2 from "./Blocks2/Blocks2";
import Blocks1 from "./Blocks1/Blocks1";
const KnowledgeBase = (props) => {
return (
<div className={style.knowledgeBase}>
<Menu forMenu={props.forKnowledgeBase}/>
<BaseAll forBaseAll={props.forKnowledgeBase}>
<Routes>
<Route path="/knowledge-base/purchase-and-refund" element={<Blocks3/>}/>
<Route path="/knowledge-base/popular-questions" element={<Blocks2/>}/>
<Route path="/knowledge-base/analytics" element={<Blocks1/>}/>
</Routes>
</BaseAll>
</div>
)
}
export default KnowledgeBase
BaseAll.js
import React from "react";
import style from './BaseAll.module.css'
const BaseAll = (props) => {
return (
<div className={style.baseAll}>
{props.children}
</div>
)
}
export default BaseAll
Menu.js (it has all NavLink)
import React from "react";
import style from './Menu.module.css'
import {NavLink} from "react-router-dom";
const Menu = (props) => {
return (
<div className={style.menu}>
<header>{props.forMenu.menu.header}</header>
<form action="">
<div>
<img src={props.forMenu.menu.search_img} alt=""/>
<input type="text" placeholder={props.forMenu.menu.input}/>
</div>
<button type={"submit"}>{props.forMenu.menu.search_btn}</button>
</form>
<nav>
<NavLink to={"/knowledge-base/analytics"} className={style.menu_NavLink}>{props.forMenu.menu.nav1}</NavLink>
<NavLink to={"/knowledge-base/popular-questions"} className={style.menu_NavLink}>{props.forMenu.menu.nav2}</NavLink>
<NavLink to={"/knowledge-base/purchase-and-refund"} className={style.menu_NavLink}>{props.forMenu.menu.nav3}</NavLink>
</nav>
</div>
)
}
export default Menu
I expect the relevant blocks to appear under the search menu when navigating through NavLink. I tried to implement many options in App.js and KnowledgeBase.js but couldn't find one that would work
In KnowledgeBase.js file, remove /knowledge-base from each route.
React router automatically adds the parent route path with each child. You do not have to write the parent path with each child if it's already placed inside with another route i.e. /knowledge-base/*
const KnowledgeBase = (props) => {
return (
<div className={style.knowledgeBase}>
<Menu forMenu={props.forKnowledgeBase}/>
<BaseAll forBaseAll={props.forKnowledgeBase}>
<Routes>
<Route path="/purchase-and-refund" element={<Blocks3/>}/>
<Route path="/popular-questions" element={<Blocks2/>}/>
<Route path="/analytics" element={<Blocks1/>}/>
</Routes>
</BaseAll>
</div>
)
}
I have a Link in my Footer.js, and for some reason, it's not working as links in the browser. Like not showing up a pointer on the link etc. I have multiple Links in my Nav component. They all are working fine, but if I place a Link in any other component, it doesn't work. I have tried using BrowserRouter instead of HashRouter and NavLink instead of Link, but the problem still exists. It seems like a reckless mistake, though.
For now, I have this as a static website. That's why I haven't wrapped my components in Switch and Route.
Footer.js
import { Link } from 'react-router-dom'
const Footer = () => {
return (
<div>
<h1>Create group!</h1>
<Link to="/" className="rounded">Get Started</Link>
</div>
)
}
export default Footer
App.js:
import Footer from './Footer'
import { HashRouter } from 'react-router-dom'
const App = () => {
return (
<HashRouter basename="/">
<Nav />
<Home />
<About />
<HowItWorks />
<GroupCarousel />
<EventCarousel />
<Footer />
</HashRouter>
);
}
export default App;
index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
Any help would be appreciated.
You have to define your routes inside HashRouter using Switch and Route tags which is to be used inside the app. Otherwise your routes won't work. Try following code and it will work as expected.
Footer.js
import { Link } from 'react-router-dom'
const Footer = () => {
return (
<div>
<h1>Create group!</h1>
<Link to="/" className="rounded">Get Started</Link>
</div>
)
}
export default Footer
App.js
import Footer from './Footer'
import { HashRouter } from 'react-router-dom'
const App = () => {
return (
<HashRouter basename="/">
<Switch>
<Route path="/" exact component={Home} />
</Switch>
</HashRouter>
);
}
export default App;
Home.js
import React from 'react'
...
const Home = () => {
return (
<>
<Nav />
<Home />
<About />
<HowItWorks />
<GroupCarousel />
<EventCarousel />
<Footer />
</>
):
}
export default Home;
Hi i am new developer in ReactJS. I have a problem and want to learn how to make routing for dashboard. My root component is App component and it calls only dashboards of my pages. But I want to call different components inside of dashboard such as Login , Register etc. When I use "Link" in anywhere, How can I do this ? Could you help me at this issue?
index.tsx for BrowserRouter
import {BrowserRouter} from "react-router-dom";
const app =<App /> ;
const container = (
<>
<BrowserRouter>
{app}
</BrowserRouter>
</>
);
my router App.tsx component:
import { Route, Switch } from "react-router-dom";
import LoginDash from "../containers/login/LoginDash"
class App extends Component {
render() {
return (
<div className="App">
<Switch>
<Route path="/Login" exact component={LoginDash}></Route>
<Route path="/" exact component={LoginDash}></Route>
</Switch>
</div>
);
}
}
export default App;
My dashboard LoginDash.tsx:
import {BrowserRouter as Router, Route,Switch} from "react-router-dom"
import LoginPart from "../../components/login/left/LoginPart";
import RegisterPart from "../../components/login/left/RegisterPart";
import AnitamionPart from '../../components/login/right/AnimationPart';
export const Login = () => {
return (
<div className="login-container">
<div className="row login-row" >
<div className="login-left-part" >
<Router>
<Switch>
<Route exact path = "/Login" component={LoginPart}></Route>
<Route exact path = "/Register" component={RegisterPart}></Route>
</Switch>
</Router>
</div>
<div className=" login-right-part" >
<AnitamionPart></AnitamionPart>
</div>
</div>
</div>
)
}
export default Login;
First of all, you only need to use one instance of BrowserRouter in your App, Since you already wrap App component with BrowserRouter that would be enough
Secondly, if you specify exact attribute on Routes, no nested Routes will ever match. Make sure you don't use Route with exact prop if the component has any nested Route defined
Lastly, you only need to render LoginDash on / route, /Login handling can be done separately as a nested Route
Update your individual components like below
import {BrowserRouter} from "react-router-dom";
// Do not render app as a constant separetely,
const container = (
<>
<BrowserRouter>
<App />
</BrowserRouter>
</>
);
import { Route, Switch } from "react-router-dom";
import LoginDash from "../containers/login/LoginDash"
class App extends Component {
render() {
return (
<div className="App">
<Route path="/" component={LoginDash}></Route>
</div>
);
}
}
export default App;
export const Login = () => {
return (
<div className="login-container">
<div className="row login-row" >
<div className="login-left-part" >
<Switch>
<Route exact path = "/Login" component={LoginPart}></Route>
<Route exact path = "/Register" component={RegisterPart}></Route>
</Switch>
</div>
<div className=" login-right-part" >
<AnitamionPart></AnitamionPart>
</div>
</div>
</div>
)
}
export default Login;
Working demo
I'm creating a personal website to train with React and i have been stuck since a couples of days to render a component with React Router like i will render a blog post when i click on the card.
Eveytime i click on the card instead i got the correct data but it's om the bottom of the main page and i would like to open it dynamically on a new page, like if i click on an article of a blog or a news.
My card component
render() {
const { match } = this.props;
const { data, value } = this.state;
return (
<>
<div>
{data.map((job, id) => (
<div key={id}>
<div key={job._id} className="blog-card">
<div className="meta">
<div className="photo">
<img src={job.img} alt="logo" />{" "}
</div>
</div>
<div className="description">
<p> {job.description}</p>
<p className="read-more">
<p>{job.location}</p>
<p>
<span className="apply-job">
{" "}
<Link
className="link-apply"
to={{
pathname: `${match.url}/${job._id}`,
state: job
}}
>
go to {job.workplace_name}
</Link>{" "}
</span>
</p>
</p>
</div>
</div>
</div>
))}
</div>
}
<Route path={`${match.path}/:_id`} component={Articles} />
</>
);
}
}
When i click on go to {job.workplace_name} i would like to render the component below on a new page and not under my card component
const Articles = ({ location }) => (
<div>
<h1>Hello</h1>
<h1>{location.state.workplace_name}</h1>
<h2>{location.state.position_name}</h2>
<h2>{location.state.description}</h2>
<h2>{location.state.Compensation}</h2>
</div>
)
export default Articles;
When you code your app with react-router you should have a top-level component that is in charge of deciding which screen to print based on the url you are visiting.
It should look like this
import { BrowserRouter } from "react-router-dom";
import { Route, Switch } from "react-router";
const App = () => (
<Switch>
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route path="/invoices/:id" component={Invoice} />
</Switch>
);
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
el
);
The Switch component will render only the first Route that matches the current URL.
If you don't use it, every route that match will be rendered.
The is useful at the top level to avoid having two screens at the same time (kinda like your issue).
Your biggest issue is that you put the route responsible for the display of a blog page inside your card component.
Your component hierarchy:
Router
└ App
└ Route+Home (maybe)
└ Card
└ Route+Articles
What it should be instead:
Router
└ App
└ Switch
├ Route+Home (maybe)
│ └ Card
└ Route+Articles
Also when you new to change the location (url) you can use the Link component as you did, or use history props that you get from the Route component.
In the example above, Dashboard and Invoice get the "history" property.
history.push(path) will simulate navigation
history.replace(path) will simulate redirection.
and there's more https://reacttraining.com/react-router/core/api/history
So presumably, you have an index.js file that looks like this or should look like this where you added <BrowserRouter> in accordance to the solution:
import { BrowserRouter } from "react-router-dom";
import { Route, Switch } from "react-router";
const App = () => (
<Switch>
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route path="/invoices/:id" component={Invoice} />
</Switch>
);
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
el
);
But, what if your index.js file looked like this instead:
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore, applyMiddleware, compose } from "redux";
import reduxThunk from "redux-thunk";
import App from "./components/App";
import reducers from "./reducers";
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
reducers,
composeEnhancers(applyMiddleware(reduxThunk))
);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.querySelector("#root")
because you created an index.js file that is at the top-level of your component hierarchy and then an App.js file, that looks like this:
import React from "react";
import { Route } from "react-router-dom";
import Dashboard from "./invoices/Dashboard";
import Invoice from "./invoices/Invoice";
import Header from "./Header";
const App = () => {
return (
<div className="ui container">
<div>
<Header />
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route path="/invoices/:id" component={Invoice} />
</div>
</div>
);
};
export default App;
In such a case, you can use the exact keyword on all your Routes and encapsulate it all inside of <BrowserRouter> and ensure you have that encapsulated in <div>s like so:
import React from "react";
import { BrowserRouter, Route } from "react-router-dom";
import Dashboard from "./invoices/Dashboard";
import Invoice from "./invoices/Invoice";
import Header from "./Header";
const App = () => {
return (
<div className="ui container">
<BrowserRouter>
<div>
<Header />
<Route exact path="/invoices/dashboard" component={Dashboard} />
<Route exact path="/invoices/:id" component={Invoice} />
</div>
</BrowserRouter>
</div>
);
};
export default App;