how to click on h1 header to navigate to component? - reactjs

I am trying to change the header to a link that calls for Homepage component. Right now it displays a logo title. But I want my users to be able to click on it to direct them to the landing page .
import React from 'react';
import { NavLink } from 'react-router-dom';
import AuthContext from '../../context/auth-context';
import './MainNavigation.css';
// import Homepage from '../../pages/Homepage';
const mainNavigation = props => (
<AuthContext.Consumer>
{context => {
return (
<header className="main-navigation">
<div className="main-navigation__logo">
<h1>Lux Lab Hair Salon</h1> // ....... change this to a link
</div>
<nav className="main-navigation__items">
<ul>
<li>
<NavLink to="/events">Available Hours</NavLink>
</li>
{!context.token && (
<li>
<NavLink to="/auth">Sing In | Sing Up</NavLink>
</li>
)}
{context.token && (
<React.Fragment>
<li>
<NavLink to="/bookings">Bookings</NavLink>
</li>
<li>
<button onClick={context.logout}>Logout</button>
</li>
</React.Fragment>
)}
</ul>
</nav>
</header>
);
}}
</AuthContext.Consumer>
);
export default mainNavigation;

<h1><NavLink to={'...'}>Lux Lab Hair Salon</NavLink></h1> or just a <NavLink style={{ fontSize: '20px' }}>Lux Lab Salon</NavLink>

So the problem is how to call for a component in a react navigation bar.
Here are the steps I followed to resolve the problem.
I imported BrowserRouter in App.js and used it to define the path of the homepage
in App.js
import {BrowserRouter, Route, Redirect, Switch} from 'react-router-dom';
import Homepage from './pages/Homepage';
class App extends Component {
render() {
return (<BrowserRouter>
<React.Fragment>
<Navigation/>
<Switch>
<Route path="/" component={Homepage}/>
<Route path="/nav1path" component={Page1Component}/>
<Route path="/nav2path" component={Page2Component}/>
{!this.state.token && <Redirect to="/" exact/>}
</Switch>
</React.Fragment>
</BrowserRouter>);
}
}
export default App;
Then I used the suggestion above to circle my H1 tag with NavLink.
<NavLink to={'/'}>
Header title
Also don't forget to import other components before you call them inside Route.

Related

React JS Development Build Showing a White Screen

I am using a MacBook Pro with Big Sur 11.6.4 and with Node 17.6.0. Before the White Screen my Code was like this:
Navbar.component.jsx
import React from 'react';
import {
Nav,
NavLink,
Bars,
NavMenu,
NavBtn,
NavBtnLink,
} from './NavbarElements';
const Navbar = () => {
return (
<>
<Nav>
<Bars />
<NavMenu>
<NavLink to='/' activeStyle>
Home
</NavLink>
<NavLink to='/about' activeStyle>
About
</NavLink>
<NavLink to='/browse' activeStyle>
Browse
</NavLink>
<NavLink to='/sign-up' activeStyle>
Sign Up
</NavLink>
</NavMenu>
<NavBtn>
<NavBtnLink to='/signin'>Sign In</NavBtnLink>
</NavBtn>
</Nav>
</>
);
};
export default Navbar
App.js
import React, { Component } from 'react';
import Navbar from './Components/Navbar/Navbar.component';
import Button from '#mui/material/Button'
import logo from './logo.svg';
import { BrowserRouter as Router } from 'react-router-dom';
import './App.css';
import Footer from './Components/Footer';
class App extends Component {
render() {
return (
<div className="App">
<Router><Navbar /></Router>
<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"
>
<Button variant="contained">Learn React</Button>
</a>
</header>
<Footer/>
</div>
)
}
}
export default App;
When I made some changes afterwards when I decided to add some routing to the Navbar.jsx file making it look like this:
import React from 'react';
import {
Nav,
NavLink,
Bars,
NavMenu,
NavBtn,
NavBtnLink,
} from './NavbarElements';
import { BrowserRouter as Route, Routes } from "react-router-dom";
import ReactDOM from 'react-dom'
import Browse from '../../Pages/Browse/Browse'
import About from '../../Pages/About/About'
const Navbar = () => {
return (
<>
<Nav>
<Bars />
<NavMenu>
<NavLink to='/' activeStyle>
Home
</NavLink>
<NavLink to='/about' activeStyle>
About
</NavLink>
<NavLink to='/browse' activeStyle>
Browse
</NavLink>
<NavLink to='/sign-up' activeStyle>
Sign Up
</NavLink>
</NavMenu>
<NavBtn>
<NavBtnLink to='/signin'>Sign In</NavBtnLink>
</NavBtn>
<Routes>
<Route path="/" />
<Route path="/about" element={<About />} />
<Route path="/browse" element={<Browse />} />
</Routes>
</Nav>
</>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<Navbar />, rootElement);
export default Navbar
Error Part 1
Error Part 2
Is there anything I can do to fix this?
Im Sorry if this is a silly mistake. I am just starting to learn react. My apologies if this might feel like a waste of time.
I fixed it.
STEP 1
I just had to remove all other instance of <Route path='' element={}> and anything related to it on other files in the project.
STEP 2
Next, I needed to reformat the App.js making it look like this:
import {BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Home from './Pages/Home/Home.jsx';
import Browse from './Pages/Browse/Browse.jsx'
import About from './Pages/About/About.jsx'
import Footer from "./Components/Footer.jsx";
import Navbar from "./Components/Navbar/Navbar.component.jsx";
import './App.css';
function App() {
return (
<Router>
<Navbar />
<Routes>
<Route path='/' exact component={<Home/>} />
<Route path='/browse' component={<Browse/>} />
<Route path='/about' component={<About/>} />
</Routes>
<Footer/>
</Router>
);
}
export default App;
RESULT
I ended up with two errors left and one GOOD warning:
Warning
So, Thats it.

How can I hide component (Navbar) on landing page

How can I hide the navbar on my landing page('/')? I have tried using this.props.location and extending the navbar component and that dose not seem to work. I know this is simple but I can not for the life of me remember how to do it! I would rather not render the component just on the pages that I want to display it.
App.js
import React from 'react'
import Navbar from './Components/Navbar'
import {BrowserRouter as Router, Route} from 'react-router-dom';
import Home from './Components/Home'
import Welcome from './Components/Welcome'
import About from './Components/About'
import Contact from './Components/Contact'
function App() {
return (
<>
<useLocation>
{({ useLocation }) => {
if (useLocation.pathname !== "/") { return <Navbar/>; } }
}
</useLocation>
<Router>
<Route exact path="/" component={Welcome}/>
<Route path="/Home" component={Home}/>
<Route path="/About" component={About}/>
<Route path='/Whitepaper' component={WhitePaper}/>
</Router>
</>
);
}
export default App;
Navbar.js
import React from 'react';
import { Link } from 'react-router-dom';
import "../Component Stylesheets/Navbar.css"
const Navbar = (props) =>{
return(
<>
<nav className='navbar'>
<Link to="/Home">
<div className="logo"></div>
</Link>
<Link to="/Home">
<li className="nav-item nav-link">Home</li>
</Link>
<Link to="/About">
<li className="nav-item nav-link">About</li>
</Link>
<Link to="/Contact">
<li className="nav-item nav-link">Contact</li>
</Link>
</nav>
</>
)
}
export default Navbar
You can check what page it is and show it then. I think you mentioned trying this but you just might have been doing it the wrong way.
{props.location.pathname !== '/' ? <Navbar /> : null}
you need to import Location from your react-router.
And use the Location data before your <Router> block.
Below is a sample from other react router library which should work same way for react-router as well:
<Location>
{({ location }) => {
if (location.pathname !== "/") { return <NavBar/>; } }
}
</Location>
or you can make use of useLocation Hooks
like below to get the current path and exclude NavBar component
let location = useLocation();

Render a different component inside a div when Link is clicked "react-router-dom" v5

I'm trying to render a component inside my dashboard component by clicking a Link in that component,
I need to render Home or Contacts or About component inside my div
should I use array to store the components I want to render or route? adn how do I do it?
import React, { useContext, useEffect, useState, Fragment } from "react";
import { BrowserRouter as Router, Route, Switch, Link } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Contacts from "./Contacts";
const DashBoard = () => {
const authContext = useContext(AuthContext);
return (
<div>
<div> // this is my side nav
<Route>
<ul>
<li>
<Link to="/dashboard/home">Home</Link>
</li>
<li>
<Link to="/">Issues</Link>
</li>
<li>
<Link to="/">Contacts</Link>
</li>
<li>
<Link to="/dashBoard/about">About</Link>
</li>
</ul>
</Route>
</div>
<div>
<nav>
<button
className="btn btn-primary"
id="menu-toggle"
onClick={() => setIsToggled(!isToggled)}
>
Toggle Menu
</button>
</nav>
<div className="container-fluid">
// i need to render a component here ex.) <Home/> | <Contacts/> | <About/> when a Link is clicked!
</div>
</div>
</div>
);
};
export default DashBoard;
in my app.js
<Fragment>
<Navbar />
<div className="">
<Switch>
<Route exact path="/dashBoard" component={DashBoard} />
<Route exact path="/login" component={Login} />
</Switch>
</div>
</Fragment>

Hiding some navigation bar links in some of the react components: React+ React-Router+Typescript

I am a bit new to React.
I have a situation where I need to hide some navigation bar links in some components where as in the rest, I want to display them.
Actually been using react router V4 and typescript.
Need to hide page1 and page2 links when it comes to signup and login pages.
Say I also have a getstarted page that loads when the application is launched , here also I would like to hide the links.
Show the links in rest of the components.
Help would be appreciated
Router Code
import * as React from 'react';
import NavBar from './NavBar';
import SignUp from './SignUp';
import Page1 from './Page1';
import Page2 from './Page2';
import Login from './Login';
import GetStarted from './GetStarted';
import { BrowserRouter as Router , Switch , Route} from 'react-router-dom';
const NotFound = () => (
<div><h1>404.. This page is not found!</h1></div>
);
export default class App extends React.Component<{}, {}> {
render() {
return(
<Router>
<div className='container'>
<NavBar/>
<div className='body'>
<Switch>
<Route exact={true} path='/' component={GetStarted}/>
<Route path='/getstarted' component={GetStarted}/>
<Route path='/signup' component={SignUp}/>
<Route path='/login' component={Login}/>
<Route path='/page1' component={Page1}/>
<Route path='/page2' component={Page2}/>
<Route component={NotFound} />
</Switch>
</div>
</div>
</Router>
)
}
}
Navigation Bar Component
import React from 'react';
import { Link } from 'react-router-dom';
export default class NavBar extends React.Component<{}, {}> {
render() {
return (
<nav className="nav">
/*some logo will be displayed here followed by the links*/
<div className="container">
<ul className="item-wrapper">
<li>
<Link to="/page1">Link 1</Link>
</li>
<li>
<Link to="/page2">Link 2</Link>
</li>
</ul>
</div>
</nav>
);
}
}
Since you provide a login function, surely somewhere in your state you store the information whether a user is logged in or not. Use this state to determine whether to display the links:
import React from 'react';
import { Link } from 'react-router-dom';
export default class NavBar extends React.Component<{}, {}> {
render() {
return (
<nav className="nav">
<div className="container">
{user.loggedIn /* boolean indicating whether user is logged in */ &&
<ul className="item-wrapper">
<li>
<Link to="/page1">Link 1</Link>
</li>
<li>
<Link to="/page2">Link 2</Link>
</li>
</ul>
}
</div>
</nav>
);
}
}

activeClassName of react-router v4 not working appropriately

I am very new to react-routing. After reading the docs and reading some articles this is how structured my routing. Please correct me if I am doing wrong.
Using react-router V4
Routes.js
import React from 'react';
import App from '../app/components/App';
import Dashboard from '../dashboard/components/Dashboard';
import Contact from '../dashboard/components/Contact';
import Account from '../dashboard/components/Account';
import Career from '../dashboard/components/Career';
import NoMatch from './NoMatch';
import { Provider } from 'react-redux';
import { Route, BrowserRouter, Switch, Redirect } from 'react-router-dom';
const Root = ({ store }) => (
<Provider store={store}>
<BrowserRouter>
<div>
<Route path="/app" component={App} />
<Switch>
<Route path="/app" exact component={Dashboard} />
<Route path="/app/home/dashboard" component={Dashboard} />
<Route path="/app/home/contact" component={Contact} />
<Route path="/app/home/account" component={Account} />
<Route path="/app/home/career" component={Career} />
<Route component={NoMatch} />
</Switch>
</div>
</BrowserRouter>
</Provider>
)
export default Root
I used /app 2 times. First is to load always as it has sidenav and header. Then inside switch I used to load default component dashboard.
App.js
import React, { Component } from 'react';
import 'bootstrap/dist/css/bootstrap.css';
import 'font-awesome/css/font-awesome.min.css';
import Header from './Header';
import SideNav from './SideNav';
class AppComp extends Component {
componentDidMount() {
const { match: { params } } = this.props;
}
render() {
return (
<div>
<div className="container body">
<div className="main_container">
<Header />
<SideNav routeparams={this.props}/>
</div>
</div>
</div>
);
}
}
export default AppComp;
Sidenav.jsx
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom'
import '../../css/sidenav.css';
class SideNav extends Component {
render() {
console.log(this.props.routeparams);
return (
<div className="col-md-3 left_col">
<div className="left_col">
<div className="clearfix"></div>
<div id="sidebar-menu">
<div className="menu">
<ul className="nav side-menu">
<li>
<NavLink to="/app/home/dashboard">Home</span></NavLink>
<ul>
<li className="current-page">
<NavLink to="/app/home/dashboard" activeClassName="current">Dashboard</NavLink>
</li>
<li>
<NavLink to="/app/home/contact" activeClassName="current">Contact</NavLink>
</li>
<li>
<NavLink to="/app/home/account" activeClassName="current">Account</NavLink>
</li>
<li>
<NavLink to="/app/home/career" activeClassName="current">Career</NavLink>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</div>
);
}
}
export default SideNav;
I have two issue :
this.props.routeparams in sidenav logged twice which means sidenav rendered twice . this is happening after adding routing . Also this.props.routeparams match path is always /app, which I think because sidenav is a child component of app component. How can I fix this ? I want to get current route path in sidenav.
activeClassName="current" gets applied to correct navlink but the css style gets reflected only if I click somewhere in the page. Seems so strange. I can resolve that issue if I get current match.path at sidenav component then I will do it custom way without activeClassName.
Any help would be greatly appreciated.

Resources