I am using Materialize UI with React and I am trying to make the navbar on top fixed, however, I am getting a big gap on the left-hand side of the navbar that pushes the entire navbar itself to the right (see photo).
How can I make that gap in front disappear? Sorry, if this is a silly question but it's my first time trying to build an actual web app with React.
Thanks.
// App.js
import React from 'react';
import { BrowserRouter, Route } from 'react-router-dom';
import Header from './Header';
const App = () => {
return (
<BrowserRouter>
<Route path="/"/>
<Header />
</BrowserRouter>
)
};
export default App;
// Header.js
import React from 'react';
import { Link } from 'react-router-dom';
const Header = () => {
return (
<div className='container'>
<div className="navbar-fixed">
<nav className="teal">
<div className="nav-wrapper">
<Link to="#home" className="brand-logo">Travelville</Link>
<Link to="#" data-activates="mobile-nav" className="button-collapse">
<i className="material-icons">menu</i>
</Link>
<ul className="right hide-on-med-and-down">
<li>
<Link to="#home">Home</Link>
</li>
<li>
<Link to="#search">Search</Link>
</li>
<li>
<Link to="#popular">Popular Places</Link>
</li>
<li>
<Link to="#gallery">Gallery</Link>
</li>
<li>
<Link to="#contact">Contact</Link>
</li>
</ul>
</div>
</nav>
</div>
<ul className="side-nav" id="mobile-nav">
<li>
<Link to="#home">Home</Link>
</li>
<li>
<Link to="#search">Search</Link>
</li>
<li>
<Link to="#popular">Popular Places</Link>
</li>
<li>
<Link to="#gallery">Gallery</Link>
</li>
<li>
<Link to="#contact">Contact</Link>
</li>
</ul>
</div>
);
};
export default Header;
Related
I've just started using reactJS and i am integrating an HTML admin template in react which has header, sidebar, footer and the main section content.
One thing i'm a bit curious is whenever i click links in the main section content, only the specific section dynamically loads, but when i click on links in sidebar the entire page reloads, why is it so? Below is the code that i'm following
App.js
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import Header from './Components/Header';
import Content from './Components/Content';
import Footer from './Components/Footer';
import Sidebar from './Components/Sidebar';
const App = () => {
return(
<BrowserRouter>
<Header/>
<Sidebar/>
<Content/>
<Footer/>
</BrowserRouter>
);
}
export default App;
Header.jsx
import React from 'react';
import logo from '../stack.png';
const Header = () => {
return(
<nav>
</nav>
);
}
export default Header;
Sidebar.jsx
import React from 'react';
import { Link } from 'react-router-dom';
const Sidebar = () => {
return(
<aside>
<div className="main-menu menu-fixed menu-dark menu-accordion menu-shadow" data-scroll-to-active="true">
<div className="main-menu-content">
<ul className="navigation navigation-main" id="main-menu-navigation" data-menu="menu-navigation">
<li style={{'margin': '25px 0 0 0'}} className="active nav-item">
<Link to="/dashboard">
<i className="ft-home"></i>
<span className="menu-title">
Dashboard
</span>
</Link>
</li>
<li className="nav-item"></i><span className="menu-title" data-i18n="">Create</span>
<ul className="menu-content">
<li className=" menu-item">
<Link to="/create">
<span className="menu-title">
Create New
</span>
</Link>
</li>
<li className=" menu-item">
<Link to="/createxyz">
<span className="menu-title">
Create XYZ
</span>
</Link>
</li>
</ul>
</li>
</ul>
</div>
</div>
</aside>
);
}
export default Sidebar;
Content.jsx
import React from 'react';
import { Switch, Route } from 'react-router-dom';
import Dashboard from './Dashboard';
import Enter from './Enter';
import Enterxyz from './Enterxyz';
const Content = () => {
return(
<Switch>
<Route exact path="/" component={Dashboard} />
<Route path="/dashboard" component={Dashboard} />
<Route path="/create" component={Enter} />
<Route path="/createxyz" component={Enterxyz} />
</Switch>
);
}
export default Content;
Footer.jsx
import React from 'react';
function Footer()
{
return(
<footer className="footer footer-static footer-light navbar-border">
<p className="clearfix blue-grey lighten-2 text-sm-center mb-0 px-2">
<span className="float-md-left d-block d-md-inline-block">© 2020
<a className="text-bold-800 grey darken-2" href="">
xyz
</a> (All Rights Reserved)
</span>
</p>
</footer>
);
}
export default Footer;
Code snippet in import Enter from './Enter';
import React from 'react';
import { Link } from 'react-router-dom';
const Enter = () => {
return(
<div className="app-content content">
<div className="content-wrapper">
<div className="content-body">
<div className="row">
<div className="col-md-12">
<div className="card" style={{'height': 'auto'}}>
<div className="card-header">
<h4 className="card-title" id="basic-layout-form">Create New Client</h4>
<a className="heading-elements-toggle"><i className="fa fa-ellipsis-v font-medium-3"></i></a>
<div className="heading-elements">
<ul className="list-inline mb-0">
<li><Link to="/dashboard">Dashboard</Link></li>
<li><i className="ft-chevrons-right"></i></li>
<li><a>Create</a></li>
<li><i className="ft-chevrons-right"></i></li>
<li><Link to="/create">Enter New</Link></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
export default Enter;
in the Enter code snippet there are 2 links(Breadcrumbs) when i click on them the page loads dynamically without refresh.
But when i click on the links in sidebar, the entire page reloads to display the content, why is it so? What is wrong?
I want to for example only show the logout button if a user is actually logged in.
My _app.js file:
import React from "react";
import App from "next/app";
import Navbar from "../components/navbar";
import Layout from "../components/layouts/mainLayout";
import { handleAuthSSR } from "../utils/auth";
export default class MyApp extends App {
render() {
const { Component, pageProps, userAuth } = this.props;
return (
<Layout userAuth={pageProps.token}>
<Component {...pageProps} />
</Layout>
);
}
}
Main Layout:
import Head from "next/head";
import Navbar from "../navbar";
const Layout = props => {
return (
<>
<Head>
<title>My App</title>
<link
rel="stylesheet"
href="https://bootswatch.com/4/cerulean/bootstrap.min.css"
></link>
<link href="/static/css/styles.css" rel="stylesheet"></link>
</Head>
<Navbar />
<div className="container">{props.children}</div>
</>
);
};
export default Layout;
Navbar:
import Link from "next/link";
import { logout } from "../utils/auth";
const Navbar = props => (
<nav className="navbar navbar-expand navbar-dark bg-dark mb-4">
<div className="container">
<a className="navbar-brand" href="#">
My App
</a>
<div className="collapse navbar-collapse">
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link href="/">
<a className="nav-link">Home</a>
</Link>
</li>
<li className="nav-item">
<Link href="/about">
<a className="nav-link">About</a>
</Link>
</li>
<li className="nav-item">
<Link href="/register">
<a className="nav-link">Register</a>
</Link>
</li>
{props.userAuth ? (
<li>
<button className="btn btn-primary" onClick={logout}>
Logout
</button>
</li>
) : (
<li className="nav-item">
<Link href="/login">
<a className="nav-link">Login</a>
</Link>
</li>
)}
</ul>
</div>
</div>
</nav>
);
export default Navbar;
So, I am passing userAuth via props but I haven't actually got the logic for userAuth.
The handleAuthSSR looks like:
export const handleAuthSSR = ctx => {
const { token } = nextCookie(ctx);
if (ctx.req && !token) {
ctx.res.writeHead(302, { Location: "/login" });
ctx.res.end();
return;
}
if (!token) {
console.log("no token found");
Router.push("/login");
}
return token;
};
I am using this as part of a HOC when a user navigates around and it works but not sure how to get this to work with regards to the navigation show/hide.
I can see that you didn't pass the userAuth prop to the navbar component in mainLayout.js. Also you need to work on your Parent/Children props communications.
Maybe you can use your HOC to return token or false (or null instead of false). Use it then in navbar.js to toggle Login/Logout link.
I simply try to use react provider but somehow i get this error:
index.js:1437 Warning: A context consumer was rendered with multiple children, or a child that isn't a function. A context consumer expects a single child that is a function. If you did pass a function, make sure there is no trailing or leading whitespace around it.
this is context.js
import React, { Component } from 'react'
const Context=React.createContext()
class Providerr extends Component {
state={display:false}
displayeditor=()=>{this.setState({display:!this.state.display}) }
render() { return (
<Context.Provider value={{...this.state,displayeditor:this.displayeditor }}>
{this.props.children}
</Context.Provider>
)
}
}
const Consumer=Context.Consumer
export {Providerr,Consumer}
I want to Consume it below
import React, { Component } from 'react'
import {NavLink} from "react-router-dom"
import "./leagues.scss"
import {Consumer} from "./context.js"
export default class Navbar extends Component {
render() {
return (
<nav className="navbar navbar-expand-lg">
<a className="navbar-brand" href="#Home">
<img src="./images/customLogo.jpg" className="navlogo"/>
</a>
<div className="collapse navbar-collapse">
<ul className="navbar-nav mx-5">
<NavLink to="/">
<li className="nav-item active mr-5">
<div className="nav-link">
<span>Home</span>
</div>
</li>
</NavLink>
<Consumer> {value =>
<li onClick={value.displayeditor} className="nav-item mr-5">
<div className="nav-link modal_lig" href="">
<span>Leagues</span>
</div>
</li> }
</Consumer>
</nav>
)}}
Wrap the whole code inside Consumer as shown below. Also note that some of your html elements are not properly closed.
import React, { Component } from 'react'
import {NavLink} from "react-router-dom"
import "./leagues.scss"
import {Consumer} from "./context.js"
export default class Navbar extends Component {
render(){
return(
<Consumer>
{value => (
<nav className="navbar navbar-expand-lg">
<a className="navbar-brand" href="#Home">
<img src="./images/customLogo.jpg" className="navlogo"/>
</a>
<div className="collapse navbar-collapse">
<ul className="navbar-nav mx-5">
<NavLink to="/">
<li className="nav-item active mr-5">
<div className="nav-link">
<span>Home</span>
</div>
</li>
</NavLink>
<li onClick={()=>value.displayeditor()} className="nav-item mr-5">
<div className="nav-link modal_lig" href="">
<span>Leagues</span>
</div>
</li>
</ul>
</div>
</nav>
)}
</Consumer>
)
}
}
I'm having trouble with my Actions in Redux with my NextJS app.. I'd imagine it would be relatively similar to implementing Redux in any other React application but I can't for the life of me get my actions to fire off. It has been awhile since I've touched Redux so it might be something really obvious that I'm missing but I've spent several hours trying to troubleshoot it. When my window width gets to 600px I have a breakpoint that shows a hamburger icon and I want it to add the class "open" to my navbar. Here's my repo. https://github.com/nicer00ster/nicer00ster-blog
add an eventListener to handle window resize and then check the innerWidth of window to call your action.
import React from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { toggleMenu } from '../../actions'
import Sidebar from './Sidebar';
import Nicer00ster from '-!svg-react-loader?name=Nicer00ster!../../images/svg/nav/nicer00ster.svg';
import Smartphone from '-!svg-react-loader?name=Smartphone!../../images/svg/nav/smartphone.svg';
import House from '-!svg-react-loader?name=House!../../images/svg/nav/house.svg';
import Telephone from '-!svg-react-loader?name=Telephone!../../images/svg/nav/telephone.svg';
class Navbar exntends React.Component {
constructor() {
super();
this.onWindowResize = this.onWindowResize.bind(this);
}
componentDidMount() {
window.addEventListener('resize', onWindowResize);
}
onWinodwResize() {
if (window.innerWidth < 600 && !this.props.isOpen)
this.props.toggleMenu();
}
render() {
const {isOpen, toggleMenu} = this.props;
return (
<div>
<div className={ isOpen ? "navbar open" : "navbar" }>
<ul className="navbar__links">
<li className="navbar__item">
<Link to="/">
<Nicer00ster className="logo" width={200} height={100}/>
</Link>
<Sidebar />
</li>
</ul>
<ul className="navbar__navigation">
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/">
<a className="navbar__link">
<House className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Home</span>
</a>
</Link>
</li>
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/blog">
<a className="navbar__link">
<Telephone className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Blog</span>
</a>
</Link>
</li>
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/work">
<a className="navbar__link">
<Smartphone className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Work</span>
</a>
</Link>
</li>
<li className="navbar__item" onClick={ toggleMenu }>
<Link activeClassName="active" to="/contact">
<a className="navbar__link">
<Telephone className="navbar__item--svg" width={100} height={50} />
<span className="navbar__item--text">Contact</span>
</a>
</Link>
</li>
</ul>
</div>
<Sidebar isOpen={isOpen} />
</div>
)
}
export default connect(function(state) {
return { isOpen: state.app.open }
}, { toggleMenu })(Navbar);
I recently tried implementing a navlink like so:
import React from 'react';
import { Link, NavLink } from 'react-router-dom';
import '../css/navbar.css';
import logoWhite from '../../common/images';
const Navbar = props => {
return (
<div id="navbar" className="grid">
<Link to="/" className="column logo-container">
<img src={logoWhite} alt="Test logo" />
</Link>
<ul className="column navigation">
<li>
<NavLink to="/" exact className="nav-link" activeClassName="active">Home</NavLink>
</li>
<li>
<NavLink to="/match" className="nav-link" activeClassName="active">Partner Proposals</NavLink>
</li>
</ul>
</div>
);
};
export default Navbar;
Upon running I encountered the issue of the classname not being passed to the navlink when the route changed. Reading on articles and it led me to suspect that the shouldComponentUpdate method was being rendered false. So, I rewrote the Navbar like so:
import React from 'react';
import { Link, NavLink } from 'react-router-dom';
import '../css/navbar.css';
import logoWhite from '../../common/images';
class Navbar extends React.Component {
shouldComponentUpdate() {
console.log('Console log activated');
return true;
}
render() {
return (
<div id="navbar" className="grid">
<Link to="/" className="column logo-container">
<img src={logoWhite} alt="Test image" />
</Link>
<ul className="column navigation">
<li>
<NavLink to="/" exact activeClassName="active" className="nav-link">Home</NavLink>
</li>
<li>
<NavLink to="/match" activeClassName="active" className="nav-link">Partner Proposals</NavLink>
</li>
</ul>
</div>
);
}
}
export default Navbar;
What's strange is that the componentDidUpdate returned the value in the console (suggesting that it returned true), but the NavLink was still not passing the activeClassName
Can anyone suggest a solution to this problem?
Based on the suggestion from #mindaJalaj, I passed a pathname prop (which was rendering and re-rendering whenever there was a update to the route from the parent component), and I implemented the Navbar as follows
import React from 'react';
import PropTypes from 'prop-types';
import { Link, NavLink } from 'react-router-dom';
import { Icon } from 'semantic-ui-react';
import '../css/navbar.css';
import logo from '../../common/images/logo.png';
import navLinks from '../navLinks';
const Navbar = props => {
function renderNavLinks() {
return navLinks.map((navLink, index) => {
const key: string = `nav-link-${navLink.path}-${index}`;
const exact: boolean = navLink.exact || false;
return (
<li key={key}>
<NavLink
to={navLink.path}
exact={exact}
className="nav-link"
isActive={() => navLink.path === props.pathname}
activeClassName="active"
>
<Icon name={navLink.icon} />
<span>{navLink.title}</span>
</NavLink>
</li>
);
});
}
return (
<div id="navbar" className="grid">
<Link to="/" className="column logo-container">
<img src={logo} alt="Test logo" />
</Link>
<ul className="column navigation">{renderNavLinks()}</ul>
</div>
);
};
Navbar.propTypes = {
pathname: PropTypes.string
};
Navbar.defaultProps = {
pathname: '/'
};
export default Navbar;