Update active item in menu with React and Semantic-Ui - reactjs

How can i change an item in the menu to be set to active item?
Currently the first item "Home" is always set to be active item. The goal is that if I click on of the other items, then that item will be set to active and the other item will no longer be active.
return (
<div className="ui menu">
<Link to="/">
<img
style={{
padding: 20
}}
className="ui image"
src={Rmarketingv1}
alt="alt"
/>
</Link>
<div
style={{
margin: 40
}}
className="ui borderless horizontal right secondary menu"
>
<div className="active item">
<Link to="/">
<h2>
Home
</h2>
</Link>
</div>
<div className="item">
<Link to="/Service">
<h2>
Tjenester
</h2>
</Link>
</div>
<div className="item">
<Link to="/work">
<h2>
Jobbmuligheter
</h2>
</Link>
</div>
<div className="item">
<Link to="/contact">
<h2>
Kontakt
</h2>
</Link>
</div>
</div>
</div>
);

You can just make use of NavLink instead of Link. you can give it an activeClassName prop and whenever the route matches to prop that className will be added to that link.
read the documentation here:
https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/NavLink.md

React router feeatures a <NavLink> property which features an activeClassName property. So to make things work, change your <Links> like this:
<div className="item">
<NavLink to="/" activeClassName="active">
<h2>
Home
</h2>
</NavLink>
</div>
Disclaimer: This will set the "active" class to your element and not your div like in your example above. Make sure to adjust the css code as well!

Related

How to link to other internal sites when the list where the links are place is generated with a map function?

I just started to work with react and just getting by. Right now I am using a react website template and working on a menu list. Therefore, each menu-point (slide) should link to an individual site on my project. The thing is that the list is populated with a map function and therefore I just have one link. My question now is, where or how can I insert different links to different internal sites?
Right now all the slides point to the same page: /project-details2/project-details2-dark
{ShowcassesFullScreenData.map((slide) => (
<SwiperSlide key={slide.id} className="swiper-slide">
<div className="full-width">
<div
className="bg-img valign"
style={{ backgroundImage: `url(${slide.image})` }}
data-overlay-dark="1"
>
<div className="caption ontop valign">
<div className="o-hidden">
<h1 data-swiper-parallax="-2000">
<Link
href={`/project-details2/project-details2-dark`}
passHref
>
<a>
<div className="stroke">{slide.title.first}</div>
<span>{slide.title.second}</span>
</a>
</Link>
</h1>
</div>
</div>
<div className="copy-cap valign">
<div className="cap">
<h1 data-swiper-parallax="-2000">
<Link
href={`/project-details2/project-details2-dark`}
passHref
>
<a>
<div className="stroke">{slide.title.first}</div>
<span>{slide.title.second}</span>
</a>
</Link>
</h1>
</div>
</div>
</div>
</div>
</SwiperSlide>
))}
The solution you need is called "routing". There are many solutions to this but I recommend using popular package React Router (https://reactrouter.com/)
export default function App() {
return (
<div>
<h1>Bookkeeper</h1>
<nav>
<Link to="/invoices">Invoices</Link> |{" "}
<Link to="/expenses">Expenses</Link>
</nav>
<Outlet/>
</div>
);
}
or the Next.js framework (https://nextjs.org/)
export default function Home() {
return (
<div className="container">
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1 className="title">
Read{' '}
<Link href="/posts/first-post">
<a>this page!</a>
</Link>
</h1>
</main>
</div>
)
}

Opening new tab upon clicking image for ReactJS

I'm trying to open a new link (in this case, i've just included a dummy link : facebook.com) on a new tab upon clicking on the hover-img class. But I'm facing some trouble doing it. Below is a snippet of my code, would gladly appreciate the help.
<div className="container">
{data.map((d)=>(
<div className="item">
<img className="background-img" src={d.img} ></img>
<h3>{d.title}</h3>
<img
className="hover-img"
href="www.facebook.com"
src="asset/eye.png"
/>
</div>
))}
</div>
Instead of setting the href attribute, you could open a new window with window.open() on click.
<div className="container">
{data.map((d)=>(
<div className="item">
<img className="background-img" src={d.img} ></img>
<h3>{d.title}</h3>
<img
className="hover-img"
src="asset/eye.png"
alt="eye"
onClick={()=> window.open("https://www.facebook.com", "_blank")}
/>
</div>
))}
</div>

After clicking on Link in Gatsby whole website is stuck

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.

Next.js onhover Link component gets refreshed

I am having an ecommerce application build in next.js. In this I have top bar contains contact and other link information, below that there is a long and search section. This two items are done in 2 components and both are combined.
Now the problem is, on hover any link in components, the particular page in refreshed (not reloading). What may be the problem. For better understanding I have placed a video blow. Please check.
https://www.dropbox.com/s/tlh4e2os1ok2tpr/screen-recording.webm?dl=0
I have given all link as below,
<Link href="/" as="/">
<a className="mr-4">Help Center</a>
</Link>
Please check my below codes,
Top.js
const Top = () => {
return (
<div>
<div className="topBar">
<div className="container">
<div className="row">
<div className="col-sm-4 col-md-6">
<p className="text-center text-sm-left">
<Link href="/" as="/">
<a>Sell in RangerBee</a>
</Link>
</p>
</div>
<div className="col-sm-8 col-md-6">
<p className="text-center text-sm-right">
<Link href="/" as="/">
<a className="mr-4">Gift Card</a>
</Link>
<Link href="/" as="/">
<a className="mr-4">Help Center</a>
</Link>
<Link href="/" as="/">
<a>Download app</a>
</Link>
</p>
</div>
</div>
</div>
</div>
</div>
}
export default Top;
Header.js
const Header = () => {
return (
<div>
<div className="searchBar">
<div className="container-fluid">
<div className="row align-items-center">
<div className="col-8 col-md-2 col-lg-2">
<p>
<Link href="/" as="/">
<a><img src="/vercel.svg" alt="Example" className="img-fluid" /></a>
</Link>
</p>
</div>
<div className="col-4 col-md-2 col-lg-3 col-xl-2">
<div className="menuCategory">
<p className="main" onClick={() => changeMenuCategory()}><span>Shop By Category</span> <FontAwesomeIcon icon={faChevronDown} className="ml-2 pt-1 fa-down"/> <FontAwesomeIcon icon={faBars} className="ml-2 pt-1 fa-bars"/></p>
<ul className={toggleCategory ? "main-nav" : "main-nav d-none"}>
{menus && menus.data.map((cat, index) => (
<li key={index}>
<Link href={"/products/"+cat.cat_slug} as={"/products/"+cat.cat_slug}><a title={cat.cat_name}>{cat.cat_name} <FontAwesomeIcon icon={faChevronRight} className="ml-2 float-right menu-arrow"/></a></Link>
<ul>
{cat.subcategory.map((subcat, index1) => (
<li key={index1+index}><Link href={"/products/"+cat.cat_slug+"/"+subcat.sub_slug} as={"/products/"+cat.cat_slug+"/"+subcat.sub_slug}><a title={subcat.sub_name}>{subcat.sub_name}</a></Link></li>
))}
</ul>
</li>
))}
</ul>
</div>
</div>
<div className="col-12 col-md-4 col-lg-4 col-xl-5 mt-3 mb-3 mt-md-0 mb-md-0">
<div className="search-box">
<div className="search-inner">
<input type="text" name="q" placeholder="Search for products, brands and much more" onClick={() => setToggleSearch(b => !b)} onFocus={(e) => trendingSearch(e)} onChange={(e) => startSearch(e)} />
<button><FontAwesomeIcon icon={faSearch}/></button>
</div>
<div className={toggleSearch ? "search-result" : "search-result d-none"}>
<ul ref={dropdown}>
{trending !== null && searched === null && trending.data.length > 0 && <li className="trending">Trending Search</li>}
{trending !== null && searched == null && trending.data.length > 0 &&
trending.data.map((trending, index) => (
<li key={index}><Link href={"/products/"+trending.trending_slug} as={"/products/"+trending.trending_slug}><a>{trending.trending_name}</a></Link></li>
))
}
{searched !== null && searched.data.length > 0 &&
searched.data.map((search, index) => (
<li key={index}>
<Link href={search.search_url} as={search.search_url}>
{search.images.length > 0 ?
<a><img src={api.IMAGE_PRODUCTS+"/"+search.images[0].image_name} alt={search.search_name} /> {search.search_name}</a>
:
<a>{search.search_name}</a>
}
</Link>
</li>
))
}
</ul>
</div>
</div>
</div>
<div className="col-12 col-md-4 col-lg-3">
<div className="text-center text-md-right">
<div className="d-inline loginDrop">
<Link href={!isAuthenticated ? "/login" : "/profile"} as={!isAuthenticated ? "/login" : "/profile"}>
<a className="signinBtn mr-5">{!isAuthenticated ? "Sign In" : "My Account"}</a>
</Link>
<div className={!isAuthenticated ? "login-content" : "login-content logout-content"}>
<p> </p>
<div className="login-inner">
<Link href={!isAuthenticated ? "/login" : "/profile"} as={!isAuthenticated ? "/login" : "/profile"}><a><FontAwesomeIcon icon={faUserCircle} className="mr-2"/> Your Profile</a></Link>
<Link href={!isAuthenticated ? "/login" : "/orders"} as={!isAuthenticated ? "/login" : "/orders"}><a><FontAwesomeIcon icon={faBoxOpen} className="mr-2 orderIcon"/> Orders</a></Link>
<Link href={!isAuthenticated ? "/login" : "/wishlist"} as={!isAuthenticated ? "/login" : "/wishlist"}><a><FontAwesomeIcon icon={faHeart} className="mr-2"/> Whishlist</a></Link>
<div className="otherDrop">
{!isAuthenticated ?
<>
<p className="first">Don't have an account?</p>
<p className="register"><Link href="/register" as="/register"><a>Register</a></Link></p>
<p className="login"><Link href="/login" as="/login"><a>Login</a></Link></p>
</>
:
<p className="login"><a href="#" onClick={deauthenticate}>Logout</a></p>
}
</div>
</div>
</div>
</div>
<Link href="/cart" as="/cart">
<a className="cartBtn"><FontAwesomeIcon icon={faShoppingCart} className="mr-xl-1"/> Cart <span>{carts[0] === null ? 0 : carts.length}</span></a>
</Link>
</div>
</div>
</div>
</div>
</div>
</div>
}
export default Header;
Layout.js
import Head from 'next/head';
import * as Main from './Index'
import './styles/Index';
const Layout = ({ children, title }) => (
<div>
<Head>
<title>{ title }</title>
<meta charSet="utf-8" />
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
<Main.Top/>
<Main.Header/>
<div>
{ children }
</div>
</div>
);
export default Layout;
next.config.js
const withSass = require('#zeit/next-sass')
module.exports = withSass({
cssLoaderOptions: {
url: false
}
})
Is there any problem with my code?
I am using sass for styling. What may be the issue. I have searched a lot and couldn't find any solution. Actually am new to next.js. So I am not able to find a solution.
As said in their docs, next/Link accept the props href and as, where:
href: The page that it should go for, this path will never change at runtime.
as: Something like a decorator, how the link should appears for the user. It can change in runtime.
Tha been said, lets take a look in one of yours Link's:
<Link href={"/products/"+trending.trending_slug} as={"/products/"+trending.trending_slug}><a>{trending.trending_name}</a></Link>
In this case your href is changing dynamic with your trending and it should be a page that exists in your project, like pages/product/trending and if trending is dynamic you can use pages/product/[trending] and then in the as property you should use the dynamic link, like you are already doing, /products/"+trending.trending_slug.
Take a look into their docs about it: https://nextjs.org/docs/tag/v9.5.2/api-reference/next/link#dynamic-routes
Here is a issue that look's to be the same problem: https://github.com/vercel/next.js/issues/11157

React smooth scroll only partially working

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."

Resources