React smooth scroll only partially working - reactjs

I am trying to implement a smooth scroll in React, with no jquery. I have downloaded and installed react-scrollable-anchor from npm. The smooth scroll works with the first two items in my menu, and not with the last 2. See my website nav.
<div className="App">
<div>
<div className="header">
<div className="logobox">
<img className="logo" src="/eplogo.png" alt="logo"/>
</div>
<nav className="nav">
<a className="navitem" href="#projects">
<p>Projects</p>
</a>
<a className="navitem" href="#about">
<p>About</p>
</a>
<a className="navitem" href="#resume">
<p>Resume</p>
</a>
<a className="navitem" href="#contact">
<p>Contact</p>
</a>
</nav>
</div>
<header className="heading">
<a className="name" href="/">
<h1 className="App-title">Emily Pancake</h1>
</a>
<h2 className="tagline" >Web Developer and Design Enthusiast</h2>
<div className={this.state.ghost}>
<p className={this.state.typewriter}>{this.state.jokeText}</p>
</div>
<a className="arrowDown" href="#projects" rel="noopener noreferrer"><Button shape="circle" icon="down" /></a>
<ScrollableAnchor id={'projects'}>
<h2 className="sectionTitle notop">Projects</h2>
</ScrollableAnchor>
</header>
<section className="projectList">
<Projects projects={projects} key="2" />
</section>
<ScrollableAnchor id={"about"} name="about"><About key="1"/></ScrollableAnchor>
<ScrollableAnchor id={"resume"} name="resume"><Resume key="2"/></ScrollableAnchor>
<ScrollableAnchor id={"contact"} name="contact"><Footer key="1"/></ScrollableAnchor>
</div>
</div>
How can I get it to work on "resume" and "contact." The only difference is that they are functional react components instead of classes. The error seems to be related: "Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail."

Related

How to create a new page with React js

I have a code where this would essentially show links that you can press and would redirect you. I am have a problem where I am trying to click the link to "eco-news" and essentially open up a page with news articles in it. I have coded the news section, but I just dont know how to implement it.
Home > News > (directed to a new page where news would would populate)
import React from "react";
import './sidebar.css';
//import News from "../news section/News";
import { Link } from 'react-router-dom'
// Imported Images ==============>
import logo from '../../Assets/logo.png';
// Imported icons ===============>
import {IoMdSpeedometer} from 'react-icons/io';
import {TfiWorld} from 'react-icons/tfi';
import {BsQuestionCircle} from 'react-icons/bs'
// placeholder logo image
const Sidebar = () => {
return (
<div className="sideBar grid">
<div className="logoDiv flex">
<img src={logo} alt="World Space Logo" />
<h2>World Space.</h2>
</div>
<div className="menuDiv">
<h3 className="divTitle">
CONSUMPTION OVERVIEW
</h3>
<ul className="menuLists grid">
<li className="listItem">
<a href="/#" className="menuLink flex">
<IoMdSpeedometer className="icon" />
<span className="smallText">
Readout
</span>
</a>
</li>
<li className="listItem">
<a href="/#" className="menuLink flex">
<TfiWorld className="icon" />
<span className="smallText">
Driven Equivalent
</span>
</a>
</li>
<li className="listItem">
<a href="/#" className="menuLink flex">
<TfiWorld className="icon" />
<span className="smallText">
Efficiency Score
</span>
</a>
</li>
</ul>
</div>
{/* This is the tab for the Eco news, please update the necessary fonts and spacing */}
<div className="menuDiv">
<h3 className="divTitle">
NEWS
</h3>
<ul className="menuLists grid">
<li className="listItem">
{/* <a href=/News className="menuLink flex"> */}
<Link to ='\News' className="menuLink flex">
<TfiWorld className="icon" />
<span className="smallText">
Eco-News
</span>
</Link>
{/* </a> */}
</li>
</ul>
</div>
<div className="settingsDiv">
<h3 className="divTitle">
SETTINGS
</h3>
<ul className="menuLists grid">
<li className="listItem">
<a href="/#" className="menuLink flex">
<IoMdSpeedometer className="icon" />
<span className="smallText">
Dashboard
</span>
</a>
</li>
<li className="listItem">
<a href="/#" className="menuLink flex">
<TfiWorld className="icon" />
<span className="smallText">
Consumption Overview
</span>
</a>
</li>
<li className="listItem">
<a href="/#" className="menuLink flex">
<TfiWorld className="icon" />
<span className="smallText">
Goals
</span>
</a>
</li>
<li className="listItem">
<a href="/#" className="menuLink flex">
<TfiWorld className="icon" />
<span className="smallText">
Achievements
</span>
</a>
</li>
</ul>
</div>
<div className="sideBarCard">
<BsQuestionCircle className="icon" />
<div className="cardContent">
<h3>Help Center</h3>
{/*<p>Having trouble in World Space, please contact us.</p>
<button className="btn">Go to help center</button>*/}
</div>
</div>
</div>
);
}
export default Sidebar;
<div className="menuDiv">
<h3 className="divTitle">
NEWS
</h3>
<ul className="menuLists grid">
<li className="listItem">
{/* <a href=/News className="menuLink flex"> */}
<Link to ='\News' className="menuLink flex">
<TfiWorld className="icon" />
<span className="smallText">
Eco-News
</span>
</Link>
{/* </a> */}
</li>
</ul>
</div>
if you click the link it would lead to a broken page.
What it looks like so far
Ensure that you have followed the below steps as pre requisites
Create browser router
Add a route for your News component
Only then will your routing work

React Dom Outlet Route Confusion. Nest 3 Part Page Into one Route. React Router v6

I'm having a hard time understanding how the routes and outlets work in react. My problem is this.
If I have a route that should load 3 parts.
-Top Menu
-Side Menu
-Center Content
But the center content is contained in the div thats wrapped by the "Side Menu" how do I wrap another element that will be changing inside of a previous component. My goal is to use the first 2 in all of the routes and based on the route the center content will change.
This is what it should look like
<div id="layoutSideNav">
<div id="layoutsidenav_nav"></div>
<div id="layoutSidenav_content"></div>
</div>
But for some reason all I can get is this
<div id="layoutSideNav">
<div id="layoutsidenav_nav"></div>
</div>
<div id="layoutSidenav_content"></div>
My react looks like this.
3 Components
Contains the top nav
contains the side nav
contains body elements that will change.
App()
<Route
path='/admin-dashboard'
element={
<>
<AdminNavigation />
<AdminNavigationLeft />
<AdminDashBoardHome />
</>
}
>
If I add <Outlet/> before the closing div in "layoutSideNav" component. nothing happens. If I remove the outlets from all components the app() looks the same. The top nav and side nave display correclty, but the center content is wonky.
I feel like routes is so much more abstract than I'm used to so maybe I'm just not thinking about it logically. Thank you so much for your help!
I tried using <Outlet/> to adjust how where the third component content displayed.
I've added my full code below for review.
const AdminNavigation = () =>{
const toggleSideMenu = event => {
// šŸ‘‡ļø toggle class on click
document.getElementsByTagName('body')[0].classList.toggle('sb-sidenav-toggled');
};
return (
<Fragment>
<nav className="sb-topnav navbar navbar-expand navbar-dark bg-dark">
<a className="navbar-brand ps-3" href="index.html">Woodmoore Improvement</a>
<button className="btn btn-link btn-sm order-1 order-lg-0 me-4 me-lg-0" onClick={toggleSideMenu} id="sidebarToggle" href="#!"><i className="fas fa-bars"></i></button>
<form className="d-none d-md-inline-block form-inline ms-auto me-0 me-md-3 my-2 my-md-0">
<div className="input-group">
<input className="form-control" type="text" placeholder="Search for..." aria-label="Search for..." aria-describedby="btnNavbarSearch" />
<button className="btn btn-primary" id="btnNavbarSearch" type="button"><i className="fas fa-search"></i></button>
</div>
</form>
<ul className="navbar-nav ms-auto ms-md-0 me-3 me-lg-4">
<li className="nav-item dropdown">
<a className="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i className="fas fa-user fa-fw"></i></a>
<ul className="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li><a className="dropdown-item" href="#!">Settings</a></li>
<li><hr className="dropdown-divider" /></li>
<li><a className="dropdown-item" href="#!">Logout</a></li>
</ul>
</li>
</ul>
</nav>
</Fragment>
)
}
export default AdminNavigation;
const AdminDashBoardHome = () => {
return(
<Fragment>
<div id='layoutSidenav_content'>
<main>
<div className='container-fluid px-4'>
<h1 className='mt-4'>HOA Member Manager</h1>
<ol className='breadcrumb mb-4'>
<li className='breadcrumb-item active'>Member Map</li>
</ol>
<div className='row'>
<div className='col-lg-12'>
<div className='embed-responsive embed-responsive-16by9'>
<div id='map_canvas' className='embed-responsive-item pb-4' >
</div>
</div>
</div>
</div>
<div className='row'>
</div>
<div className='card mb-4'>
<div className='card-header'>
<i className='fas fa-table me-1'></i>
Member Lookup
</div>
<MemberDataList/>
</div>
</div>
</main>
<footer className='py-4 bg-light mt-auto'>
<div className='container-fluid px-4'>
<div className='d-flex align-items-center justify-content-between small'>
<div className='text-muted'>Copyright Ā© Your Website 2022</div>
<div>
<a href='#'>Privacy Policy</a>
Ā·
<a href='#'>Terms & Conditions</a>
</div>
</div>
</div>
</footer>
</div>
</Fragment>
)
}
export default AdminDashBoardHome;
const AdminNavigationLeft = () => {
document.body.classList.add('sb-nav-fixed');
return(
<Fragment>
<div id="layoutSidenav">
<Outlet/>
<div id="layoutSidenav_nav">
<nav className="sb-sidenav accordion sb-sidenav-dark" id="sidenavAccordion">
<div className="sb-sidenav-menu">
<div className="nav">
<div className="sb-sidenav-menu-heading">Core</div>
<a className="nav-link" href="index.html">
<div className="sb-nav-link-icon"><i className="fas fa-tachometer-alt"></i></div>
Dashboard
</a>
<div className="sb-sidenav-menu-heading">Interface</div>
<a className="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#collapseLayouts" aria-expanded="false" aria-controls="collapseLayouts">
<div className="sb-nav-link-icon"><i className="fas fa-columns"></i></div>
Layouts
<div className="sb-sidenav-collapse-arrow"><i className="fas fa-angle-down"></i></div>
</a>
<div className="collapse" id="collapseLayouts" aria-labelledby="headingOne" data-bs-parent="#sidenavAccordion">
<nav className="sb-sidenav-menu-nested nav">
<a className="nav-link" href="layout-static.html">Static Navigation</a>
<a className="nav-link" href="layout-sidenav-light.html">Light Sidenav</a>
</nav>
</div>
<a className="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#collapsePages" aria-expanded="false" aria-controls="collapsePages">
<div className="sb-nav-link-icon"><i className="fas fa-book-open"></i></div>
Admin Management
<div className="sb-sidenav-collapse-arrow"><i className="fas fa-angle-down"></i></div>
</a>
<div className="collapse" id="collapsePages" aria-labelledby="headingTwo" data-bs-parent="#sidenavAccordion">
<nav className="sb-sidenav-menu-nested nav accordion" id="sidenavAccordionPages">
<a className="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#pagesCollapseAuth" aria-expanded="false" aria-controls="pagesCollapseAuth">
Manage Users
</a>
<a className="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#pagesCollapseAuth" aria-expanded="false" aria-controls="pagesCollapseAuth">
Create New
</a>
</nav>
</div>
</div>
</div>
<div className="sb-sidenav-footer">
<div className="small">Logged in as:</div>
Start Bootstrap
</div>
</nav>
</div>
</div>
</Fragment>
)
}
export default AdminNavigationLeft;
If the following is what you intend to be rendered:
<div id="layoutSideNav">
<div id="layoutsidenav_nav"></div>
<div id="layoutSidenav_content"></div>
</div>
Then I suspect you want the routed content to be rendered in the layoutSidenav_content div. Something like the following:
<div id="layoutSideNav">
<div id="layoutsidenav_nav">
... navbar content ...
</div>
<div id="layoutSidenav_content">
... routed content ...
</div>
</div>
In other words, the above snippet describes a layout component, the routed content is the logical place for an Outlet component wrapping nested routes to output their element content into. The route may look like:
const AdminLayout = () => (
<>
<AdminNavigation />
<div id="layoutsidenav">
<div id="layoutSidenav_nav">
<AdminNavigationLeft />
</div>
<div id="layoutsidenav_content">
<Outlet />
</div>
</div>
</>
);
<Route
path='/admin-dashboard'
element={<AdminLayout />}
>
<Route index element={<AdminDashBoardHome />} />
... other admin routes
</Route>
const AdminNavigationLeft = () => {
useEffect(() => {
document.body.classList.add('sb-nav-fixed');
}, []);
return (
<nav className="sb-sidenav accordion sb-sidenav-dark" id="sidenavAccordion">
<div className="sb-sidenav-menu">
...
</div>
</nav>
)
}
const AdminDashBoardHome = () => {
return (
<Fragment>
<main>
...
</main>
<footer className='py-4 bg-light mt-auto'>
...
</footer>
</Fragment>
)
}
I solved my own question.
You need to run the props through the component you're trying to display. The solution should be like this.
App()
<Route path='/admin-dashboard' element={<><AdminNavigation /><AdminNavigationLeft><AdminDashBoardHome/></AdminNavigationLeft></>}>
AdminDashLeft()
const AdminNavigationLeft = (props) => {
{props.children} // This is where the child component should display
}

2 dropdowns, connected with each other, ReactJs

I am struggling with one idea and I need help.
I have a dropdown and dropup list and they contain the same 2 images(flags). In dropup list I select 1 flag and I want in other list to automaticlly be selected the remaining flag.
For now I have hard coded images but eventually I want to display them dynamically.
return (
<div className="container">
<div className={isClicked ? "div2" : "div1"}>
<div class="dropup">
<button class="dropbtn">Dropup</button>
<div class="dropup-content">
<a href="#">
<img src={logoEn} alt="logo" />
</a>
<a href="#">
<img src={logoDe} alt="logo" />
</a>
</div>
</div>
<input />
</div>
<div id="switch">
<button onClick={this.toggleBox}>
<img src={iconSwitch} alt="logo" />
</button>{" "}
</div>
<div className={isClicked ? "div1" : "div2"}>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">
<img src={logoEn} alt="logo" />
</a>
<a href="#">
<img src={logoDe} alt="logo" />
</a>
</div>
</div>
<input />
</div>
</div>
I want this to be solved in ReactJS, if anybody have some idea how to solve please let me know.
Br
C

Bulma Navbar component is not showing

I am reading Bulma docs, took example to apply it on Codesandbox but the NavBar has a problem.
I imported Bulma as mentioned in docs, copied code, and I suppose Bulma is only css framework so no js to add.
The examples of Hero section that I am using can be found over here
The example provided with Hero head and Hero body and includes Nav bar is not working. The Navbar only as you see example here
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import "bulma/css/bulma.css";
function App() {
return (
<div className="App">
<section class="hero is-primary is-medium">
<div class="hero-head">
<nav class="navbar">
<div class="container">
<div class="navbar-brand">
<a class="navbar-item">
<img
src="https://bulma.io/images/bulma-type-white.png"
alt="Logo"
/>
</a>
<span
class="navbar-burger burger"
data-target="navbarMenuHeroA"
>
<span />
<span />
<span />
</span>
</div>
<div id="navbarMenuHeroA" class="navbar-menu">
<div class="navbar-end">
<a class="navbar-item is-active">Home</a>
<a class="navbar-item">Examples</a>
<a class="navbar-item">Documentation</a>
<span class="navbar-item">
<a class="button is-primary is-inverted">
<span class="icon">
<i class="fab fa-github" />
</span>
<span>Download</span>
</a>
</span>
</div>
</div>
</div>
</nav>
</div>
<div class="hero-body">
<div class="container has-text-centered">
<h1 class="title">Title</h1>
<h2 class="subtitle">Subtitle</h2>
</div>
</div>
<div class="hero-foot">
<nav class="tabs">
<div class="container">
<ul>
<li class="is-active">
<a>Overview</a>
</li>
<li>
<a>Modifiers</a>
</li>
<li>
<a>Grid</a>
</li>
<li>
<a>Elements</a>
</li>
<li>
<a>Components</a>
</li>
<li>
<a>Layout</a>
</li>
</ul>
</div>
</nav>
</div>
</section>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Bootstrap 4 NavBar with React <Link/> component

IĀ“m trying to build a Boostrap 4 NavBar that will use ReactJS <Link> component from react-router instead of the original <li> class.
Currently IĀ“m having troubles building the component correctly, as at the end the ReactJS component has not the expected behaviour.
LetĀ“s start from the original Boostrap 4 NavBar example:
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
This is being migrated to a ReactJS component as the following:
import React from 'react';
import Link from 'react-router-dom/Link';
function Menu() {
return (
<div>
<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">
<span className="nav-item active">
<Link className="nav-link" to="/">Home <span className="sr-only">(current)</span></Link>
</span>
<span className="nav-item">
<Link className="nav-link" to="/counter">Counter</Link>
</span>
<span className="nav-item">
<Link className="nav-link disabled" to="/about">About</Link>
</span>
</ul>
<form className="form-inline my-2 my-lg-0">
<input className="form-control mr-sm-2" type="text" placeholder="Search" />
<button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
</div>
);
}
export default Menu;
Altough it runs, many of the formatting is lost and some features like "nav-link disabled" are not working (you can click on About and it goes to the desired page).
IĀ“m sure IĀ“m doing the conversion wrong, so help is appreciated here to fix the component code so that to reflect correctly the original behaviour without React.
Maybe you got it resolved. Here's a working example with Bootstrap 4:
import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Route, Switch, Link } from 'react-router-dom';
const AppPage = () => <h1>React Router 4 & Bootstrap 4</h1>
const HomePage = () => <h1>Welcome Home !</h1>
const AboutPage = () => <h1>About Us</h1>
const OneMorePage = () => <h1>One More Page !</h1>
const PrimaryLayout = () => (
<div>
<nav className="navbar navbar-expand-md navbar-dark bg-dark">
<Link className="navbar-brand" to="/">App</Link>
<button type="button" className="navbar-toggler" data-toggle="collapse" data-target="#navbarNav">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav mr-auto">
<li className="nav-item"><Link className="nav-link" to="/home">Home</Link></li>
<li className="nav-item"><Link className="nav-link" to="/about">About</Link></li>
<li className="nav-item"><Link className="nav-link" to="/one">More</Link></li>
</ul>
</div>
</nav>
<div className="container-fluid">
<Switch>
<Route path="/" exact component={AppPage} />
<Route path="/home" component={HomePage} />
<Route path="/about" component={AboutPage} />
<Route path="/one" component={OneMorePage} />
</Switch>
</div>
</div>
)
const App = () => (
<BrowserRouter>
<PrimaryLayout />
</BrowserRouter>
)
render(<App />, document.getElementById('root'))
For this particular use case, there's an easy solution.
Simply add this to your css:
.nav-link.disabled { pointer-events: none }.
That said, I'd strongly recommend to use a library of bootstrap-based react components on the long term. I personally like https://reactstrap.github.io/.
The problem is Bootstrap has a Javascript component as well which will not work in React as it clashes with React's Javascript. The CSS portion works which is what you are seeing.
You should use something like React-Bootstrap- https://react-bootstrap.github.io/. These are all the Bootstrap components written as React components so you can use them in the standard React manner by importing and using them in your JSX.

Resources