I have a reactJS application where I provide a bootstrap menu to navigate the application. In my home page, this is the code of the menu that provides the user the link to log out:
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="nav navbar-nav">
<li class="active">Logout</li>
<li className="nav-item">
<a className="nav-link" onClick={this.accountinformation}>Basic Account Information</a>
</li>
</ul>
</div>
When the user clicks on the Logout link, the application will route to preventback. This is the code in preventback.js:
import React from 'react';
class preventback extends React.Component {
componentWillMount() {
location.href="/logout";
}
render() {
return (
<div>
</div>
);
}
}
export default preventback
And this is the code in my logout.js:
import React from 'react';
import '../styles/app.css';
class logout extends React.Component {
render() {
return (
<div className="App">
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="#"> </a>
</nav>
<div className="container">
<div className="row">
<div className="col-12 text-center">
<p className="h4">You have been logged out</p>
<br /><br /><br />
<div className="text-center py-4 mt-3">
<button type="button" className="btn btn-primary"
onClick={() => {
location.href = ('/')
}}>
Log In
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default logout
All this code works as expected. When I click on the logout link, I get directed to preventback and then preventback directs me to logout.
My issue is that when I click on the browser back button, the browser takes me from the logout page to my home page. I had anticipated it taking me from the logout page to the preventback page. Then, when the preventback page loads, it would automatically route back to the logout page.
What am I not understanding?
Thanks.
Related
I want to render user name in the navbar if the user is logged in. If I use Normal Text inside h1, like User I get that in the browser but when I'm using {user.name} it's not getting me the name of user from database.
I have tried like this
import React from "react";
function Navbar() {
const user = JSON.parse(localStorage.getItem("currentUser"));
return (
<div>
<nav class="navbar navbar-expand-lg">
<a class="navbar-brand" href="#">
Book Rooms
</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
{user ? (
<>
<h1 style={{color: 'white'}}>user</h1>
</>
) : (
<>
<li class="nav-item active">
<a class="nav-link" href="/register">
Register
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/login">
Login
</a>
</li>
</>
)}
</ul>
</div>
</nav>
</div>
);
}
export default Navbar;
Try updating your <h1> tag to
<h1 style={{color: 'white'}}>{user.name}</h1>
First make sure to console.log the user before component rendering to confirm that the user object is present in the localStorage :
const user = JSON.parse(localStorage.getItem("currentUser"));
console.log(user);
if you get a good looking result in the console then procede to use in in any part of the rendered component like so
{user}
for example:
return (
....
..
.
<h1 style={{color: 'white'}}>USERNAME: {user.name}</h1>
...
..
.
)
at this point if you cannot see the element on screen it's most likely due to bad css, perhaps the element is out of screen or hidden. From the ChromeDevtools Inspect panel search for USERNAME (given that you used my example from the previous line) and use the mouse to highlight where is the h1 located; fix the CSS accordingly.
I'm creating a static website using reactjs so I downloaded a template and I divide it on components using reactjs but the responsive collapse navdar doesn't work anymore and I'm trying to fix it so I tried many solutions but I got nothing.
There are no errors but the code still doesn't work.
can you help me please!
this is the navbar component.
import React, {useState,useEffect} from 'react';
import '../assets/css/style.css';
import '../assets/css/slick.css';
import '../assets/css/magnific-popup.css';
import '../assets/css/animate.css';
import '../assets/css/bootstrap.min.css';
import '../assets/css/home7fonts.css';
import Logo from '../assets/images/logos/logo-black.png'
function Header()
{
const [toggleMenu, setToggleMenu] = useState(false)
const [screenWidth, setScreenWidth] = useState(window.innerWidth)
const toggleNav = () => {
setToggleMenu(!toggleMenu)
}
useEffect(() => {
const changeWidth = () => {
setScreenWidth(window.innerWidth);
}
window.addEventListener('resize', changeWidth)
return () => {
window.removeEventListener('resize', changeWidth)
}
}, [])
return (
<header className="main-header header-seven">
<div className="header-upper">
<div className="container-fluid clearfix">
<div className="header-inner d-flex align-items-center">
<div className="logo-outer">
<div className="logo"><img src={Logo} alt="Logo" title="Logo"></img></div>
</div>
<div className="nav-outer clearfix d-flex align-items-center">
<nav className="main-menu navbar-expand-lg">
<div className="navbar-header">
<div className="mobile-logo py-15">
<a href="index.html">
<img src={Logo} alt="Logo" title="Logo"></img>
</a>
</div>
<button onClick={toggleNav} type="button" className="navbar-toggle" data-toggle="collapse"
data-target=".navbar-collapse">
<span className="icon-bar"></span>
<span className="icon-bar"></span>
<span className="icon-bar"></span>
</button>
</div>
<div className="navbar-collapse collapse clearfix">
{(toggleMenu || screenWidth > 500) && (
<ul className="navigation onepage clearfix">
<li>Home</li>
<li>about</li>
<li>services</li>
<li>FAQs</li>
<li>pricing</li>
<li>testimonial</li>
<li>blog</li>
</ul>
)}
</div>
</nav>
<div className="header-number">
<i className="fas fa-phone-alt"></i>
<div className="number-content">
<span>Tell Us</span>
+12 ) 235 - 586 - 56
</div>
</div>
<div className="menu-btn">
<a href="#" className="theme-btn style-eight">Start Free Tral
<i className="fas fa-long-arrow-alt-right"></i></a>
</div>
</div>
</div>
</div>
</div>
</header>
);
}
export default Header;
Go to https://getbootstrap.com/docs/5.1/components/navbar/ and Copy one collapse navbar you like. Past the code in your function components and change class to className, etc.
open your public folder at root directory. Find index.html. Add bootstrap Css between <head>.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
Add Bootstrap JS after <div id="root">,
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
then, reload your react project. See it work.
I am using UIkit css framework with Gatsby.js
When I use OffCanvas (Nav) to go to different page next page reloads but it stucks. nothing is happening not even scroll.
This is my SideBar.jsx file
I have hosted the partial built website.You can check the problem. Go to
https://jainsons.netlify.app/aboutus
then open Sidebar and then click "Home"
import React from 'react'
import {Link} from 'gatsby'
import logo from "../images/logo.png"
import UIkit from 'uikit/dist/js/uikit.min.js'
import Icons from 'uikit/dist/js/uikit-icons.js'
function SideBar() {
UIkit.use(Icons)
return (
<React.Fragment>
<div className="uk-position-absolute">
<button
className="uk-button uk-button-small uk-button-secondary uk-margin-small-left " style={{marginTop:"27px"}} type="button" uk-toggle="target: #offcanvas-nav-primary">
<span className="" uk-icon="icon:menu;ratio:2"></span>
</button>
</div>
<div id="offcanvas-nav-primary" uk-offcanvas="mode:reveal;overlay: true">
<div className="uk-offcanvas-bar uk-flex uk-flex-column">
<ul className="uk-nav uk-nav-primary uk-nav-center uk-margin-auto-vertical">
<li className=" uk-margin-xlarge-bottom">
<Link to="/" >
<img src={logo} alt="LOGO"/>
</Link>
</li>
<li>
<Link to="/" className="uk-text-lead" activeClassName="uk-active" >
Home
</Link>
</li>
<li className="uk-nav-divider"></li>
<li className="uk-parent">
<p className="uk-text-lead">Brands</p>
<ul className="uk-nav-sub">
<li><p>RAPCA</p></li>
<li><p>POLYX</p></li>
</ul>
</li>
<li className="uk-nav-divider"></li>
<li>
<Link to="/about" className="uk-text-lead" activeClassName="uk-active" >
Home
</Link>
</li>
</ul>
</div>
</div>
</React.Fragment>
)
}
export default SideBar
I don't think it'll be to do with Gatsby Link necessarily. Maybe try switching <Link to="/"> to a regular <a href="/"> tag to see if the behaviour is the same. From looking in the dev tools on Chrome, I can see that the event listener for the menu icon click disappears after a link from within the nav has been clicked.
Updated, now the problem i'm seeing the navbar with format problems, shouldn't it be taking the CSS properties from bootstrap?
I Commited the project to make it easier: https://github.com/damianaguilarcogan/pfi_front_v4
Image: https://ibb.co/fdFTb4C
i just created a project but the navbar it's not loading, for sure it's a silly mistake but i'm not beeing able to find it.
I created the project, imported bootstrap in the index.html trought css links.
I created the folder components and inside ir navbar.js.
Afterwards I selected a common template from bootstrap navbar and Finally y called it from my app.js.
I don't get to see whats wrong, it should have already worked with this steps.
app.js
import React from 'react';
import logo from './logo.svg';
import './App.css';
import navbar from './components/navbar';
function App() {
return (
<div className="App">
<navbar />
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
navbar.js
import React from 'react';
class navbar extends React.Component {
render() {
return (
<nav className="navbar navbar-toggleable-md navbar-light bg-faded">
<button className="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse"
data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<a className="navbar-brand" href="#">Navbar</a>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item active">
<a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Link</a>
</li>
<li className="nav-item">
<a className="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form className="form-inline my-2 my-lg-0">
<input className="form-control mr-sm-2" type="text" placeholder="Search"></input>
<button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
)
}
}
export default navbar;
[1]: https://i.stack.imgur.com/zk8Lf.png
React expects components to be named with an uppercase. Try renaming the navbar to Navbar.
class Navbar extends React.Component
<Navbar/>
Changing your navbar to Navbar works. In addition there is a bootstrap wrapper for React that can be useful if you are just starting now. https://react-bootstrap.github.io/
I have been trying to port an html website to Gatsbyjs and React.
When building the header compartement I get an error "Adjacent JSX elements must be wrapped in an enclosing tag" error at the line with the .
I couldn't figure out the problem, and kindly ask for help.
The reason that I am building it with stateful component is that I will add some extra functionality to it later on.
This is my code:
import React, {Component} from 'react'
import { Link } from 'gatsby'
import { graphql } from 'gatsby'
import Img from 'gatsby-image'
import get from 'lodash/get'
import PropTypes from 'prop-types'
import Image from 'components/image'
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {
windowWidth: 1081,
mobileNavVisible: false
};
}
renderNavigation() {
<div className="site-logo retina pull-left"><Link to="/"><Img file="ChefsChoiceLogo300x300_DarkBG.png" width="120px" alt="Chef's Choice Logo" draggable="true"/></Link></div>
//Site Navigation-->
<nav className="navbar pull-right">
<div className="navbar-header">
<button type="button" data-toggle="collapse" data-target=".site-navigation" className="navbar-toggle"><span className="sr-only">Gezinti Bölmesini Aç/Kapa</span><span className="fa fa-bars"></span></button>
</div>
<ul className="site-navigation">
<li className="active"><Link to="/">Anasayfa</Link>
</li>
<li><Link to="menu" >Menü</Link>
</li>
<li><Link to="contact">İletişim</Link></li>
</ul>
</nav>
}
render() {
return(
<div className="menu-style-2 responsive sticky-menu">
<div className="site-wrapper">
<header className="site-header">
<div className="container">
<div className="row">
{this.renderNavigation()}
</div>
</div>
</header>
</div>
</div>
)
}
}
export default Header
I see two issues.
1) You need to do what it says and wrap your renderNavigation jsx with only one root element...
2) You need a return statement there as well...
renderNavigation() {
return (
<React.Fragment>
<div className="site-logo retina pull-left"><Link to="/"><Img file="ChefsChoiceLogo300x300_DarkBG.png" width="120px" alt="Chef's Choice Logo" draggable="true" /></Link></div>
//Site Navigation-->
<nav className="navbar pull-right">
<div className="navbar-header">
<button type="button" data-toggle="collapse" data-target=".site-navigation" className="navbar-toggle"><span className="sr-only">Gezinti Bölmesini Aç/Kapa</span><span className="fa fa-bars"></span></button>
</div>
<ul className="site-navigation">
<li className="active"><Link to="/">Anasayfa</Link>
</li>
<li><Link to="menu" >Menü</Link>
</li>
<li><Link to="contact">İletişim</Link></li>
</ul>
</nav>
<React.Fragment>
)}
First of all, renderNavigation() doesn't seem to return anything.
Second, you are returning (?) a div and a nav side by side. These need to be wrapped in a container. You can only return one element.
renderNavigation() {
return ( <-- add this
<div className="wrapper"> <-- add this
<div className="site-logo retina pull-left"><Link to="/"><Img file="ChefsChoiceLogo300x300_DarkBG.png" width="120px" alt="Chef's Choice Logo" draggable="true"/></Link></div>
//Site Navigation-->
<nav className="navbar pull-right">
<div className="navbar-header">
<button type="button" data-toggle="collapse" data-target=".site-navigation" className="navbar-toggle"><span className="sr-only">Gezinti Bölmesini Aç/Kapa</span><span className="fa fa-bars"></span></button>
</div>
<ul className="site-navigation">
<li className="active"><Link to="/">Anasayfa</Link>
</li>
<li><Link to="menu" >Menü</Link>
</li>
<li><Link to="contact">İletişim</Link></li>
</ul>
</nav>
</div> <-- add this
); <-- add this
}
In the renderNavigation wrap everything inside divs and return as you have it in the render function.
renderNavigation() {
return(
<div>
<div className="site-logo retina pull-left"><Link to="/"><Img file="ChefsChoiceLogo300x300_DarkBG.png" width="120px" alt="Chef's Choice Logo" draggable="true"/></Link></div>
//Site Navigation-->
<nav className="navbar pull-right">
<div className="navbar-header">
<button type="button" data-toggle="collapse" data-target=".site-navigation" className="navbar-toggle"><span className="sr-only">Gezinti Bölmesini Aç/Kapa</span><span className="fa fa-bars"></span></button>
</div>
<ul className="site-navigation">
<li className="active"><Link to="/">Anasayfa</Link>
</li>
<li><Link to="menu" >Menü</Link>
</li>
<li><Link to="contact">İletişim</Link></li>
</ul>
</nav>
</div>
)
}