I am new in react js I want to implement npm i react-top-loading-bar in my react app. I am using react class function component. i want to implement that see the picture
if anyone clicks on the Navigation link then it will show a loading bar at the top
if anyone know that how to implement this please let me know, it is very helpful for me
navbar.js
import React, { useEffect } from 'react'
import { Link, useLocation } from 'react-router-dom'
import './Navbar.css'
import LoginRounded from '#mui/icons-material/LoginRounded'
import Button from '#mui/material/Button';
const Navbar = () => {
//Navbar active color change
let location = useLocation();
useEffect(() => {
}, [location]);
return (
<div>
<nav className="navbar navbar-expand-lg navbar-dark" style={{ backgroundColor: "#063970" }}>
<div className="container-fluid">
<Link className="navbar-brand" to="/">Evalue Content</Link>
<button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav me-auto mb-2 mb-lg-0">
<li className="nav-item">
<Link className={`nav-link ${location.pathname === "/" ? "active" : ""}`} to="/">Home</Link>
</li>
<li className="nav-item"><Link className={`nav-link ${location.pathname === "/service" ? "active" : ""}`} to="/service">Service</Link></li>
<li className="nav-item"><Link className={`nav-link ${location.pathname === "/contact" ? "active" : ""}`} to="/contact">contact us</Link></li>
</ul>
<Button component={Link} to="/Login" variant="contained" size="medium" startIcon={<LoginRounded />} sx={{ marginLeft: 'auto' }} >Login</Button>
</div>
</div>
</nav>
</div>
)
}
export default Navbar
App.js
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Home from './components/Home';
import Navbar from './components/Navbar';
import Contact from './components/Contact';
import Service from './components/Service'
import Login from './components/Login';
// Redirect to their dashboar
import Admin from './components/dashboard/Admin';
import Employee from './components/dashboard/Employee';
import Publisher from './components/dashboard/Publisher';
//Toast error message show
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import Reset from './components/Reset';
import Newpassword from './components/Newpassword';
//admin Routes
import Project from './components/dashboard/AdminPages/Project'
import User from './components/dashboard/AdminPages/User';
function App() {
return (
<div>
<Router>
<Navbar />
<Routes>
<Route exact path="/" element={<Home />} />
<Route exact path="/service" element={<Service />} />
<Route exact path="/contact" element={<Contact />} />
<Route exact path="/login" element={<Login />} />
<Route exact path="/reset" element={<Reset />} />
<Route exact path="/reset/:token" element={<Newpassword />} />
{/* Redirect to their dashboard */}
<Route exact path="/admin" element={<Admin />} />
<Route exact path="/employee" element={<Employee />} />
<Route exact path="/publisher" element={<Publisher />} />
{/* admin pages */}
<Route exact path="/publisher" element={<Project />} />
<Route exact path="/user" element={<User />} />
</Routes>
</Router>
<ToastContainer
position="top-right"
autoClose={4000}
hideProgressBar={false}
newestOnTop={false}
closeOnClick
rtl={false}
pauseOnFocusLoss
draggable
pauseOnHover
/>
</div>
);
}
export default App;
Loading bar is not useful thing in React, because React is one page website and all the data is shown in your website is already downloaded from the server. However, we all use this for inner peace, and it gives a good user experience.
To solve this problem, you need to follow a few steps.
Install this below given line in your terminal.
npm i react-top-loading-bar
Import Loading bar from react top.
import LoadingBar from 'react-top-loading-bar
Copy this line and paste it in your "app.js" file.
Now for loading bar we need color, progress, and onLoaderFinisher. Put this lines wherevery you want the LoadingBar, mostly people prefer below the Navbar.
<LoadingBar
color='#f11946'
progress={progress}
/>
Also, You have to add setProgress() method, which helps to change the progress.
state={
prgress:0
}
setProgress = (progress) =>{
this.setState({this.state.progress:progress})
}
Now, you have to pass this method to your component. Here, you are using multiple component such as, Home, Service, Contact, and many more. So, pass this method with every component.
i.g.
<Route exact path="/" element={<Home setProgress={this.setProgress} />} />
This is consider as a props, so now where you want to show your progress bar just use as a props.
like,
this.props.setProgress(0);
//then put your code here in middle, where your data is fetch/ shown, where your process takes a few seconds, and then
this.props.setProgress(100);
Related
[UPDATED AFTER RECOMMENDATION TO USE GH-PAGES]
I am running into issues depoloy a react-app to gh-pages with pathing/pages rendering. I'm using react-router-dom to create actual links to pages so that I can share the direct link to a specific page. I've set up all the Router, Routes, Route and Link tags and everything is working fine locally. But when it's deployed, those links are giving 404 errors and I cannot track down why.
Package.json
"homepage": "https://cpm-128.github.io/portfolio-react-router",
App.js
import React from 'react';
import PortfolioContainer from './components/PortfolioContainer';
// vdom rendering
function App() {
return (
<div>
<PortfolioContainer />
</div>
);
}
export default App;
Portfolio Container
import React, { useState } from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import NavTabs from './NavTabs';
import Header from './Header';
import Footer from './Footer';
import About from './pages/About';
import Portfolio from './pages/Portfolio';
import Contact from './pages/Contact';
import Resume from './pages/Resume';
import Cycling from './pages/Cycling';
function PortfolioContainer() {
return (
// the basename will keep the entire homepage url, even with slugs
<Router basename={process.env.PUBLIC_URL}>
<div className='portfolio-container d-flex flex-column min-vh-100'>
<NavTabs />
<Header />
<Routes>
{/* index */}
<Route
exact path='/'
element={<About />}
/>
{/* repo name
<Route
exact path='/portfolio-react-router'
element={<About />}
/> */}
{/* About */}
<Route
path='/about'
element={<About />}
/>
{/* Portfolio */}
<Route
path='/portfolio'
element={<Portfolio />}
/>
{/* Contact */}
<Route
path='/contact'
element={<Contact />}
/>
{/* Resume */}
<Route
path='/resume'
element={<Resume />}
/>
{/* Cycling */}
<Route
path='/cycling'
element={<Cycling />}
/>
</Routes>
<Footer />
</div>
</Router>
)
};
export default PortfolioContainer;
NavTabs
import React from 'react';
import { Link } from 'react-router-dom';
function NavTabs({ currentPage, handlePageChange }) {
return (
<nav>
<ul className='nav nav-tabs bg-color-primary'>
{/* ABOUT */}
<li className='nav-item'>
<Link to='/about'
onClick={() => handlePageChange('About')}
className={currentPage === 'About'
? 'nav-link active'
: 'nav-link'
}
>
About
</Link>
</li>
{/* PORTFOLIO */}
<li className='nav-item'>
<Link to='/portfolio'
onClick={() => handlePageChange('Portfolio')}
className={currentPage === 'Portfolio'
? 'nav-link active'
: 'nav-link'
}
>
Portfolio
</Link>
</li>
{/* CONTACT */}
<li className='nav-item'>
<Link to='/contact'
onClick={() => handlePageChange('Contact')}
className={currentPage === 'Contact'
? 'nav-link active'
: 'nav-link'
}
>
Contact
</Link>
</li>
{/* RESUME */}
<li className='nav-item'>
<Link to='/resume'
onClick={() => handlePageChange('Resume')}
className={currentPage === 'Resume'
? 'nav-link active'
: 'nav-link'
}
>
Resume
</Link>
</li>
{/* CYCLING */}
<li className='nav-item'>
<Link to='/cycling'
onClick={() => handlePageChange('Cycling')}
className={currentPage === 'Cycling'
? 'nav-link active'
: 'nav-link'
}
>
Cycling
</Link>
</li>
</ul>
</nav>
)
};
export default NavTabs;
[ORIGINAL]
I'm trying to deploy a multipage React app to Heroku. After spending hours trying to work with GitHub pages for this, I realized gh-pages may be unable to render the components, so I moved to Heroku. The app works locally, but nothing is displaying on the Heroku app.
I set the Heroku remote
Node.js buildpack
heroku buildpacks:set mars/create-react-app for a buildpack for React app that some internet resources suggested
The Heroku app is using eco dynos
What am I missing? Nothing is rendering. I get no error messages, just that the deploy was successful.
I have this this App.js
import './App.css';
import React from 'react'
import Navbar from './COMPONENTS/Navbar'
import Body from './COMPONENTS/Body'
import Menu from './COMPONENTS/Menu';
import './COMPONENTS/Body.css'
import Data from './COMPONENTS/Data' ;
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom";
import Cakebody from './COMPONENTS/Cakebody';
import Customize from './COMPONENTS/Customize';
import Surprise from './COMPONENTS/Surprise';
import Surpriseone from './COMPONENTS/Surpriseone';
import Birthday from './COMPONENTS/Birthday';
import Flowers from './COMPONENTS/Flower';
import Gotocart from './COMPONENTS/Gotocart';
const App = () => (
<>
{/* const { productItems } = data ; */}
<BrowserRouter>
<Navbar />
<Menu />
<Routes>
<Route className="check" path='/' element={<Body title="All Items" />}></Route>
<Route className="check" path='/cakes' element={<Cakebody title="Same Day Cake" />}></Route>
{/* <Route path="/cart" component={Gotocart} /> */}
{/* <Route className="check" path='/cart' element={<Gotocart/>}></Route> */}
<Route className="check" path='/Customize' element={<Customize title="Customization Cake" />}></Route>
<Route className="check" path='/anniversary' element={<Surprise title="Anniversary cakes" />}></Route>
<Route className="check" path='/surprise' element={<Surpriseone title="Surprise Cake" />}></Route>
<Route className="check" path='/birthday' element={<Birthday title="Birthday Cakes" />}></Route>
<Route className="check" path='/flowers' element={<Flowers title="Flowers" />}></Route>
</Routes>
</BrowserRouter>
</>
);
export default App;
And I want to click on the cart and want to move to a different page with no navbar , body and all
That page is this
import React from 'react'
import images from './assets/cake21.jpeg'
import image1 from './assets/cake53.jpeg'
import image2 from './assets/cake61.jpeg'
import image3 from './assets/cake81.jpeg'
import image4 from './assets/cake78.jpeg'
import flower16 from './assets/flower16.jpeg'
import Pricetag from './Pricetag'
import Gotocart from './Gotocart'
import { Link } from 'react-router-dom'
export default function Menu() {
return (
<div>
<div className="location sample">
<i class="fa-solid fa-location-dot favicon"></i>
We operate in NOIDA and DELHI only.
<div className="go">
<a href="Gotocart.jsx">
<i class="fa-solid fa-cart-shopping"></i>
</a>
</div>
</div>
<hr className="line" />
{/* <hr className="line" /> */}
<div className="about-us">
You can share your designs on WhatsApp
<i class="fa-brands fa-whatsapp"></i>
<a href="https://wa.me/<>" target='_blank' rel="noreferrer" className='no'></a>
we can make the same on the cake.
</div>
<hr className="line" />
<div className="main-head">
<div className="new-head">Menu</div>
<div className="info">
Click on the images to see the list below
</div>
<div className="container22">
{/* <div className="con112"> */}
<Link to='/cakes' className="menulink con112 ">
<img src={images} alt="" className='img12' />
<div className='name1' >Same Day Cakes</div>
</Link>
{/* </div> */}
</div>
</div>
</div>
)
}
What should I change in these files to move to a new page when I click on the cart icon.
Current code just works like react router and menu nad navbar are right there, I want to remove that also
Simple you can create a Layout and use that Layout whenever you need based on the pages.
Example:
Layout.js
const Layout = (props) => {
return(
<>
<Navbar />
<Menu />
{props.children}
</>
)
}
Home.js
import Layout from './Layout';
const Home = (props) => {
return(
<Layout>
<h1>Home Page </h1>
</Layout>
)
}
About.js
import Layout from './Layout';
const About = (props) => {
return(
<Layout>
<h1>About Page </h1>
</Layout>
)
}
Wrap Navbar and Menu as explained by another answer into a component like this.
const Layout = (props) => {
return(
<>
<Navbar />
<Menu />
{props.children}
</>
)
}
Wrap other components having navbar with the layout component.
<BrowserRouter>
<Navbar />
<Menu />
<Routes>
<Route className="check" path='/' element={<Layout><Body title="All Items" /></Layout>}></Route>
<Route className="check" path='/cakes' element={<Layout><Cakebody title="Same Day Cake" /></Layout>}></Route>
<Route path="/cart" component={Gotocart} />
{/* rest of the code same way */}
</Routes>
</BrowserRouter>
I'm working on a reactJS project and i want to update the page title in the top Bar
knowing that each component separately :
TopBarComponent,
SideBarComponent,
and otherPageComponents (pages contents).
I tried this :
in app.js
import './app.css';
import TopBar from "./components/TopBar/TopBar";
import SideBar from "./components/SideBar/SideBar";
import Home from "./pages/home/Home";
import Senior from "./pages/Seniors/Senior";
import Calendar from './pages/Calendar/Calendar';
import SeniorDetails from './pages/Seniors/SeniorDetails/SeniorDetails';
import {
BrowserRouter as Router,
Switch,
Route
} from "react-router-dom";
import { useEffect, useState } from 'react';
import Profile from './pages/Profile/Profile';
function App() {
const [title, setTitle] = useState('Home');
useEffect(()=>{
const data= localStorage.getItem('title');
if (data)
setTitle(JSON.parse(data));
},[]);
useEffect(() => {
localStorage.setItem('title', JSON.stringify(title));
});
return (
<Router>
<TopBar title={title} />
<div className="container" >
<SideBar setTitle={setTitle} />
<Switch>
<Route setTitle={setTitle} exact path="/" component={Home} />
<Route setTitle={setTitle} path="/senior" component={Senior} />
<Route setTitle={setTitle} path="/calendar" component={Calendar} />
<Route setTitle={setTitle} path="/profile" component={Profile} />
<Route setTitle={setTitle} path="/seniorDetails/1" component={SeniorDetails} />
</Switch>
</div>
</Router>
);
}
export default App;
and this is my TopBar.js where I consumed the title:
<nav aria-label="breadcrumb">
<ol className="breadcrumb bg-transparent mb-0 pb-0 pt-1 px-0 me-sm-6 me-5">
<li className="breadcrumb-item text-sm">
<a className="opacity-5 text-dark" href="/">
Pages
</a>
</li>
<li
className="breadcrumb-item text-sm text-dark active"
aria-current="page"
>
{title}
</li>
</ol>
<h6 className="font-weight-bolder mb-0">Dashboard</h6>
</nav>
the value of the title is saving correctly in LocalStorage but when I callback the previous component it returns the default value 'Home'.
Any solution, please ?!!
I developed App that is working good but sometimes some of component not rendering when i click on links.
urls always changes but not view.. so kindly help me And tell the permanent solution of this problems. Because I am stuck with this It's creating more issues for me..
kindly help..
import React, { Fragment } from "react";
import { useSelector } from "react-redux";
import { Link } from "react-router-dom";
import Moment from "react-moment";
const PostItems = ({
post: { _id, text, name, avatar, user, likes, comments, date }
}) => {
const auth = useSelector(state => state.auth);
return (
<Fragment>
<div className='post bg-white p-1 my-1'>
<div>
<Link to='/profile'>
<img className='round-img' src={avatar} alt='' />
<h4>{name}</h4>
</Link>
</div>
<div>
<p className='my-1'>{text}</p>
<p className='post-date'>
<Moment format='YYYY-MM-DD'>{date}</Moment>
</p>
<button type='button' className='btn btn-light'>
<i className='fas fa-thumbs-up'></i>
<span>{likes.length > 0 && <span>{likes.length}</span>}</span>
</button>
<button type='button' className='btn btn-light'>
<i className='fas fa-thumbs-down'></i>
</button>
<Link to='/post/${_id}' className='btn btn-primary'>
Discussion{" "}
<span className='comment-count'>
{comments.length > 0 && <span>comments.length</span>}
</span>
</Link>
{!auth.loading && user === auth.user._id && (
<button type='button' className='btn btn-danger'>
<i className='fas fa-times'></i>
</button>
)}
</div>
</div>
</Fragment>
);
};
export default PostItems;
RouteJs Or App
This is App compnent where all other components are imported..
import { BrowserRouter as Router, Route, Switch } from "react-router
dom";
import Navbar from "./components/layouts/Navbar";
import Landing from "./components/layouts/Landing";
import Posts from "./components/posts/Posts";
<Router>
<Navbar />
<Route exact path='/' component={Landing} />
<section className='container'>
<Alert />
<Switch>
<Route exact path='/register' component={Register} />
<Route exact path='/login' component={Login} />
<Route exact path='/profiles' component={Profiles} />
<Route exact path='/profile/:id' component={Profile} />
<PrivateRoute exact path='/dashboard' component={Dashboard} />
<PrivateRoute exact path='/posts' component={Posts} />
<PrivateRoute
exact
path='/create-profile'
component={CreateProfile}
/>
<PrivateRoute exact path='/edit-profile' component={EditProfile} />
<PrivateRoute exact path='/add-education' component={AddEducation}
/>
<PrivateRoute
exact
path='/add-experience'
component={AddExperience}
/>
</Switch>
</section>
</Router>
<Link to='/profiles'>
<img className='round-img' src={avatar} alt='' />
<h4>{name}</h4>
</Link>
<Link to={ "/post/" + id } className='btn btn-primary'>
Discussion{" "}
<span className='comment-count'>
{comments.length > 0 && <span>comments.length</span>}
</span>
</Link>
you have made mistake in spelling ,its not matching any route /profile.
there is no any routes related /post/$id ,please check that also in your route file
import React, { Component } from 'react';
import history from './history';
import {Route,Router, Switch } from 'react-router-dom';
import App from './App'
import Demo from './Demo'
class Routes extends Component {
render() {
return (
<Router history={history}>
<Switch>
<Route exact path="/" component={App}/>
<Route exact path="/demo" component={Demo}/>
</Switch>
</Router>
);
}
}
export default Routes
install history npm library and create history.js file in src folder and add this lines
import { createBrowserHistory } from 'history';
export default createBrowserHistory();
i think then it possible to route
I've created an app via create-react-app. I want to use the HashRouter functionality for my app (React Router v4), but it is not working as expected.
Actually I don't know why. I.e. when I click on the contact link, then the URL changes to the route /contacts, but the related component is not being showed. It still shows the previous loaded component. The links are placed in the Navigation component. Here is one example of a Link.
<Link key={"navigation" + bu.name + "Key"}
to="/products"
className="nav-link dropdown-item imageLink animated"
onClick={(e) => this.handleClickBU(e, bu.bu)}>{bu.name}
</Link>
This is my code of App.js:
import React, { Component } from "react";
import {
BrowserRouter as Router,
HashRouter,
Route,
Link,
Switch
} from "react-router-dom";
<HashRouter>
<div>
<Background />
<div id="wrap">
<div id="main" className="container clear-top marginBottom50px main">
<div id="content">
<Navigation
key="navBar"
languagesToShow={this.state.languagesToShow}
currentLanguage={this.state.currentLanguage}
onLanguageChange={this.handleLanguageChange.bind(this)}
onBUChange={this.handleBUChange.bind(this)}
onIndustryChange={this.handleIndustryChange.bind(this)}
onCountryChange={this.handleCountryChange.bind(this)}
/>
<Route
key={"mainPageRoute"}
path={"/"}
exact={true}
render={(routeProps) => (
<MainPage
{...routeProps}
currentLanguage={this.state.currentLanguage}
country={this.state.countryObject}
contacts={this.state.contacts}
onCountryChange={this.handleCountryChange.bind(this)}
/>
)}
/>
<Route
key={"contactRoute"}
path={"/contact"}
exact={true}
render={(routeProps) => (
<ContactList
{...routeProps}
showCountrySelection={true}
currentLanguage={this.state.currentLanguage}
country={this.state.countryObject}
contacts={this.state.contacts}
onCountryChange={this.handleCountryChange.bind(this)}
key={"contactList"}
/>
)}
/>
<Route
key={"productsRoute"}
path={"/products"}
exact={true}
render={(routeProps) => (
<Products
{...routeProps}
ref={this.props.innerRef}
key="products"
isLoading={this.state.isLoading}
country={this.state.countryObject}
currentLanguage={this.state.currentLanguage}
currentBU={this.state.currentBU}
showMainProductGroups={this.state.showMainProductGroups}
showMainProductGroupDetails={
this.state.showMainProductGroupDetails
}
showProductGroupDetails={this.state.showProductGroupDetails}
mainProductGroups={this.state.mainProductGroups}
onShowMainProductGroupDetailsChange={this.handleShowMainProductGroupDetailsChange.bind(
this
)}
onShowProductGroupDetailsChange={this.handleShowProductGroupDetailsChange.bind(
this
)}
onBUChange={this.handleBUChange.bind(this)}
onCountryChange={this.handleCountryChange.bind(this)}
/>
)}
/>
</div>
</div>
</div>
</div>
</HashRouter>;
Can anyone tell me, why the url is updated, but the component not?
UPDATE
I already searched a lot and I found this link on the react training site.
I tried different things, but no one did not solve my problem.
But when I change HashRouter to Router it works fine!!! Why!?!
Can it be an issue / bug of HashRouter?