astro react component not getting styled with styled component - reactjs

I'm importing this navbar component into an astro file, the component shows up on the page but the but the styling from styled-component is not working and it is throwing an error in my code editor that says:
Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
import React from 'react';
import { useState } from 'react';
import styled from 'styled-components'
import { AiOutlineClose, AiOutlineMenu } from 'react-icons/Ai'
import { Link } from 'react-scroll';
function Navigation() {
const [navb, setNavb] = useState(true);
const handleClick = () => setNavb(!navb) ;
const [click, setClick] = useState(true)
const handleMenu= () => setNavb(!navb)
const iconStyle = {width: "30px" , height:"30px", color: "#ffffff", }
const linkStyleDesktop = { color: "#000000", padding: "0 10px ", cursor: "pointer"}
const linkStyleMobile = { color: "#ffffff", padding: "30px 0", fontSize: "1.5rem", cursor: "pointer"}
return (
<Header className = "style">
<Container>
LOGO
<Nav className="desktop-nav">
<div className="desktop-navItems" >
<Link style = {linkStyleDesktop} to="/" smooth={true} duration={500}>
Home
</Link>
<Link style = {linkStyleDesktop} to="about" smooth={true} duration={500}>
About
</Link>
<Link style = {linkStyleDesktop} to="portfolio" smooth={true} duration={500}>
Portfolio
</Link>
<Link style = {linkStyleDesktop} to="footer" smooth={true} duration={500}>
Contact
</Link>
</div>
</Nav>
<button className = "btn" onClick = {handleClick}> {!navb ? <AiOutlineClose style={iconStyle}/>: <AiOutlineMenu style={iconStyle} /> }
</button>
</Container>
<Nav className="mobile-nav">
<div className={!navb || !click ? 'display-mobile-nav' : 'display-none'} >
<Link onClick={handleMenu} style = {linkStyleMobile} to="app" smooth={true} duration={500}>
Home
</Link>
<Link onClick={handleMenu} style = {linkStyleMobile}to="about" smooth={true} duration={500}>
About
</Link>
<Link onClick={handleMenu} style = {linkStyleMobile} to="portfolio" smooth={true} duration={500}>
Portfolio
</Link>
<Link onClick={handleMenu} style = {linkStyleMobile} to="footer" smooth={true} duration={500}>
Contact
</Link>
</div>
</Nav>
</Header>
)
}
export default Navigation;
const Header = styled.header`
background-color: #262b33;
padding: 1rem 0;
width: 100vw;
position:fixed;
z-index: 999;
.display-mobile-nav {
display: block;
}
.display-none{
display: none;
}
.desktop-nav {
display: none;
}
.mobile-nav {
.display-mobile-nav {
display:flex;
flex-direction: column;
align-items: center;
box-shadow: 10px 10px 5px 0px rgba(189,189,189,0.75);
-webkit-box-shadow: 10px 10px 5px 0px rgba(189,189,189,0.75);
-moz-box-shadow: 10px 10px 5px 0px rgba(189,189,189,0.75);
transition: opacity 5s ease-in;
}
}
`;
//container for desktop nav
const Container = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
padding:0 8rem;
font-family:Arial, Helvetica, sans-serif;
#media (max-width: 1200px){
padding: 1rem 1rem;
}
a {
color: #ffffff;
}
button {
background-color: transparent;
border: none;
}
#media (min-width: 800px){
.desktop-nav {
display: block;
}
.btn {
display: none
}
.mobile-nav {
display: none;
.display-mobile-nav {
display: none;
}
}
}
`
const Nav = styled.div`
.desktop-navItems {
display: flex;
li {
padding: 0 10px;
}
}
#media (min-width: 800px){
.mobile-nav {
display: block;
}
`;

Warning: Invalid hook call....
To resolve this error modify the first line of your code to;
export default function Navigation() {
and remove the default function export referenced lower in the code.
I haven't yet figured out why but astro.js seems to take issue
with naming convention for default exports on functional components.
I can't resolve the styling issue unfortunately.

Related

Why are my link tags delayed before activating in react

I set up a list of React Link tags in my website and when one gets clicked on, it should immediately automatically scroll down to the section the user clicks on. That works, however, for whatever reason there's about a 1-2 second delay before this action actually occurs after a click. I've been trying to figure out what is causing this but I can't figure out what the problem is.
Here is the NavBar component:
import React from "react";
import { Link } from 'react-scroll';
import { FaBars, FaTimes } from 'react-icons/fa';
import { useState, useEffect } from "react";
import './Navbar.css';
const Navbar = () => {
const [click, setClick] = useState(false);
const handleClick = () => setClick(!click);
const closeMenu = () => setClick(false);
const [fix, setFix] = useState(window.scrollY >= 950);
useEffect(() => {
const setFixed = () => {
setFix(window.scrollY >= 950);
};
window.addEventListener("scroll", setFixed);
return () => {
window.removeEventListener("scroll", setFixed);
};
}, []);
return (
<div className={fix ? 'header active' : 'header'}>
<nav className={fix ? 'navbar fixed' : 'navbar'}>
<div className='hamburger' onClick={handleClick}>
{click ? (<FaTimes size={30} style={{ color: '#ffffff' }} />)
: (<FaBars size={30} style={{ color: '#ffffff' }} />)}
</div>
<ul className={click ? "nav-menu active" : "nav-menu"}>
<li className='nav-item'>
<Link to="header" spy={true} smooth={true} offset={-100} onClick={closeMenu}>Home</Link>
</li>
<li className='nav-item'>
<Link to="aboutMe" spy={true} smooth={true} offset={-100} onClick={closeMenu}>About</Link>
</li>
<li className='nav-item'>
<Link to="portfolio" spy={true} smooth={true} offset={-100} onClick={closeMenu}>Projects</Link>
</li>
<li className='nav-item'>
<Link to="resume" spy={true} smooth={true} offset={-100} onClick={closeMenu}>Resume</Link>
</li>
</ul>
</nav>
</div>
)
}
export default Navbar;
Here is the css for the NavBar component:
.header {
position: fixed;
height: 90px;
width: 100%;
top: 0;
left: 0;
z-index: 1;
transition: .3s ease-in;
overflow: hidden;
background-color: transparent;
}
.header.active {
background-color: rgba(0,0,0,.9);
}
.header .navbar {
display: flex;
justify-content: center;
align-items: center;
margin: auto;
height: 100%;
padding: 0 1rem;
}
.header .nav-menu a {
color: #ffffff;
}
.header .nav-menu {
display: flex;
}
.header .nav-item {
padding: 1rem;
font-weight: 500;
}
.header .nav-item a:hover {
padding-bottom: 4px;
color: #d4af37;
cursor: pointer;
}
.nav-menu a.active {
color: #d4af37;
text-decoration: none;
}
.hamburger {
display: none;
}
.nav-item {
list-style: none;
}
#homeLink {
text-decoration: none;
}
#media screen and (max-width:940px) {
.header {
max-width: 100%;
background-color: rgba(0,0,0,.9);
}
.header .navbar {
max-width: 100%;
justify-content: space-between;
}
.hamburger {
display: block;
}
.nav-menu {
position: fixed;
left: -100%;
top: 90px;
flex-direction: column;
background-color: rgba(0,0,0,.9);
width: 100%;
height: 90vh;
z-index: 999;
text-align: center;
transition: .3s;
}
.nav-menu.active {
left: 0;
}
.nav-item {
margin: 1.5rem 0;
}
.header .navbar img {
width: 150px;
}
}
Played around with it a bit more. I found that the smooth={true} prop in the Link tags was what slowed it down. Not entirely sure why that was the case, but removing it made my navbar work as intended.

React Scroll Active class is not working?

Im using react-scroll and I want to get a border to my Navbar sections when they are in the sections using a className of active. But it works just for the first section.
when I inspect I just see the first li get the active class.
I tried to get them manual class by using react-scroll function activeClass=active and then set a CSS file for the class but it doesnt work either
see the img
see the img
import { Link as LinkR} from 'react-router-dom'
import { Link as LinkS} from 'react-scroll'
import styled from 'styled-components'
export const Nav = styled.nav`
background: ${({scrollNav}) => (scrollNav ? '#000' : 'transparent ')} ;
height: 80px;
margin-top: -80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1rem;
position: sticky;
top: 0;
z-index: 10;
#media screen and (max-width: 960px) {
transition: 0.8s all ease
}
`
export const NavbarContainer = styled.div`
display: flex;
justify-content: space-between;
height: 80px;
z-index: 1;
width: 100%;
padding: 0 24px;
max-width: 1100px;
align-items: center;
`
export const NavLogo = styled(LinkR)`
color: #fff;
justify-self: flex-start;
cursor: pointer;
font-size: 1.5rem;
align-items: center;
margin-left: 24px;
font-weight: bold;
text-decoration: none;
`
export const MobileIcon = styled.div `
display: none;
#media screen and (max-width: 768px) {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
color: #fff;
}
`
export const NavMenu = styled.ul `
display: flex;
align-items: center;
list-style: none;
text-align: center;
margin-right: -22px;
#media screen and (max-width: 768px){
display: none;
}
`
export const NavItem = styled.li `
height: 80px;
`
export const NavLinks = styled(LinkS) `
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0 1rem;
height: 100%;
cursor: pointer;
&.active {
border-bottom: 3px solid #01bf74;
}
`
export const NavBtn = styled.nav `
display: flex;
align-items: center;
#media screen and (max-width: 768px){
display: none;
}
`
export const NavBtnLink = styled(LinkR) `
border-radius: 50px;
background: #01bf71;
white-space: nowrap;
padding: 10px 22px;
color: #010606;
font-size: 16px;
outline: none;
border: none;
cursor: pointer;
transition: all 0.2s ease-in-out;
text-decoration: none;
&:hover {
transition: all 0.2s ease-in-out;
background: #fff;
color: #010606;
}
`
import React, { useEffect, useState } from 'react'
import { MobileIcon, Nav, NavbarContainer, NavBtn, NavBtnLink, NavItem, NavLinks, NavLogo, NavMenu } from './NavbarEelemnts'
import { FaBars } from 'react-icons/fa'
import { animateScroll as scroll } from 'react-scroll'
const Navbar = ({ togle }) => {
const [scrollNav, setScrollNav] = useState(false)
const ChangeNav = () => {
if (window.scrollY >= 80) {
setScrollNav(true)
} else {
setScrollNav(false)
}
}
useEffect(() => {
window.addEventListener('scroll', ChangeNav)
}, [])
const togleHome = () => {
scroll.scrollToTop()
}
return (
<>
<Nav scrollNav={scrollNav}>
<NavbarContainer>
<NavLogo to='/' onClick={togleHome}>Dolla</NavLogo>
<MobileIcon onClick={togle} >
<FaBars />
</MobileIcon>
<NavMenu>
<NavItem>
<NavLinks
to="about"
spy={true}
smooth={true}
offset={-80}
duration={500}
exact='true'
>
About
</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="discover" spy={true} smooth={true} offset={-80} duration={500} exact='true' >
Discover
</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="services" spy={true} smooth={true} offset={-80} duration={500} exact='true' >
Services
</NavLinks>
</NavItem>
<NavItem>
<NavLinks to="signup" spy={true} smooth={true} offset={-80} duration={500} exact='true'>
Sign Up
</NavLinks>
</NavItem>
</NavMenu>
<NavBtn>
<NavBtnLink to='/signin'>Sign In</NavBtnLink>
</NavBtn>
</NavbarContainer>
</Nav>
</>
)
}
export default Navbar

Add animation to map components in React

So i take my data from sanity then map them and then render them as components
my code is
function Projects() {
return <ProjectList projects={projectData} />;
}
export default Projects;
import ProjectItem from "./ProjectItem";
function ProjectList(props) {
return (
<ul>
{props.projects.map((project) => (
<ProjectItem
key={project.id}
id={project.id}
title={project.title}
description={project.desc}
/>
))}
</ul>
);
}
export default ProjectList;
Card,
Project,
ProjectTitle,
ProjectDesc,
ProjectUrl,
BtnWrap,
} from "./ProjecItemElements";
function ProjectItem(props) {
return (
<Project>
<Card>
<ProjectTitle>{props.title}</ProjectTitle>
<ProjectDesc>{props.description}</ProjectDesc>
<BtnWrap>
<ProjectUrl>Test</ProjectUrl>
</BtnWrap>
</Card>
</Project>
);
}
export default ProjectItem;
Card, Project, ProjectTitle, ProjectDesc, ProjectUrl, BtnWrap, are custom components so i can customize them using that
import styled from "styled-components";
export const Card = styled.div`
background-color: white;
border-radius: 6px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
`;
export const Project = styled.li`
margin: 1rem 0;
padding: 1rem;
`;
export const ProjectTitle = styled.h3`
text-align: center;
font-size: 1.25rem;
color: #2c292b;
`;
export const ProjectDesc = styled.p`
color: black;
`;
export const BtnWrap = styled.div`
text-align: right;
width: 100%;
padding: 0 1.5rem 1.5rem 0;
`;
export const ProjectUrl = styled.button`
text-decoration: none;
color: initial;
`;
what i want is to animate the components in the ul
{props.projects.map((project) => (
<ProjectItem
key={project.id}
id={project.id}
title={project.title}
description={project.desc}
/>
so they will come from the bottom to top with a delay when the page 'loads'
(its not really load because React)

React styled components with themeContext

I have the following React Header component in which I'm trying to get a theme value from the ThemeContext.Consumer and pass it into my NavListItem. It's currently working (sort of) but I'd like to only pass it once and have it applied globally. Instead of having to write theme={theme.typography.navigation} each time I use the styled component?
import React from "react"
import { Link } from "gatsby"
import PropTypes from "prop-types"
import styled from "styled-components"
import ThemeContext from "../context/ThemeContext"
const Main = styled.header`
height: 70px;
background-color: white;
position: fixed;
width: 100%;
left: 0;
`
const NavWrapper = styled.div`
position: relative;
`
const NavListLeft = styled.ul`
color: black;
position: absolute;
top: 25px;
left: 40px;
list-style: none;
margin: 0;
padding: 0;
`
const NavListItem = styled.li`
display: inline-block;
margin: 0 10px;
font-family: ${props => props.theme};
font-size: 16px;
`
const Header = ({ siteTitle }) => (
<ThemeContext.Consumer>
{theme => (
<Main>
<NavWrapper>
<NavListLeft>
<NavListItem theme={theme.typography.navigation}>
<Link style={{color: 'black'}}>Women</Link>
</NavListItem>
<NavListItem theme={theme.typography.navigation}>Men</NavListItem>
<NavListItem theme={theme.typography.navigation}>Designers</NavListItem>
<NavListItem theme={theme.typography.navigation}>Collection</NavListItem>
<NavListItem theme={theme.typography.navigation}>Sale</NavListItem>
</NavListLeft>
</NavWrapper>
</Main>
)}
</ThemeContext.Consumer>
)
Header.propTypes = {
siteTitle: PropTypes.string,
}
Header.defaultProps = {
siteTitle: ``,
}
export default Header
You could create a NavListItem that consumes your theme.
const ThemedNavListItem = styled.li`
display: inline-block;
margin: 0 10px;
font-family: ${props => props.theme.typography.navigation};
font-size: 16px;
`
const NavListItem = props => (
<ThemeContext.Consumer>
{theme => <ThemedNavListItem {...props} theme={theme} />}
</ThemeContext.Consumer>
);
const Header = ({ siteTitle }) => (
<Main>
<NavWrapper>
<NavListLeft>
<NavListItem>
<Link style={{color: 'black'}}>Women</Link>
</NavListItem>
<NavListItem>Men</NavListItem>
<NavListItem>Designers</NavListItem>
<NavListItem>Collection</NavListItem>
<NavListItem>Sale</NavListItem>
</NavListLeft>
</NavWrapper>
</Main>
);
A simpler solution using styled-components ThemeProvider.
A helper component for theming. Injects the theme into all styled
components anywhere beneath it in the component tree, via the context
API.
const NavListItem = styled.li`
display: inline-block;
margin: 0 10px;
font-family: ${props => props.theme.typography.navigation};
font-size: 16px;
`
const Header = ({ siteTitle }) => (
<ThemeProvider theme={theme}>
<Main>
<NavWrapper>
<NavListLeft>
<NavListItem>
<Link style={{color: 'black'}}>Women</Link>
</NavListItem>
<NavListItem>Men</NavListItem>
<NavListItem>Designers</NavListItem>
<NavListItem>Collection</NavListItem>
<NavListItem>Sale</NavListItem>
</NavListLeft>
</NavWrapper>
</Main>
</ThemeProvider>
);

Next js font awesome and google fonts

Hoping to get some help on an issue I have been having in Next js. I am using Fontawesome-react package for icon imports which is working fine but when I attempt to load in a google font style sheet into the head of my main component, it changes the fonts but makes the font awesome icons disappear. I have tried a couple of different solutions such as loading the font locally and utilizing _document and _app components but none fix the issue I am having. Everything ends up changing the font but still gets rid of all the font awesome icons from every part of my app. I will copy in the Nav component I have and the Layout component that holds everything.
Thank you for any help!
import React from 'react'
import Nav from './nav/Nav'
import Head from 'next/head'
import Footer from './Footer'
type Props = {
title?: string
}
const Layout: React.FC<Props> = ({ children, title = 'Macros' }) => {
return (
<div>
<Head>
<title>{title}</title>
<link
href="https://fonts.googleapis.com/css?family=Darker+Grotesque&display=swap"
rel="stylesheet"
/>
</Head>
<Nav />
<div id="pageContent">{children}</div>
<Footer />
<style jsx global>{`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h1,
h2,
h3,
h4,
h5 {
font-weight: lighter;
font-family: 'Darker Grotesque', sans-serif;
}
#pageContent {
padding: 8rem 1rem;
}
`}</style>
</div>
)
}
export default Layout
import React, { useState } from 'react'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faBars, faSearch, faPlus } from '#fortawesome/free-solid-svg-icons'
import Link from 'next/link'
const NavBar: React.FC = () => {
const [navBarStatus, isOpen] = useState<Boolean>(false)
const toggleMenu = () => {
isOpen(navBarStatus ? false : true)
}
return (
<nav id="navContainer">
<div id="mobileNav">
<ul id="navBarTop">
<li>
<button type="button" className="navIcon" onClick={toggleMenu}>
<FontAwesomeIcon icon={faBars} />
</button>
</li>
<li>
<Link href="/">
<a>
<h3>Macro</h3>
</a>
</Link>
</li>
<li>
<button type="button" className="navIcon">
<FontAwesomeIcon icon={faSearch} />
</button>
</li>
</ul>
<ul id="navbarBottom">
<li>Top</li>
<li>New</li>
<li>Protein</li>
<li>Carbs</li>
<li>Fats</li>
</ul>
</div>
<div id="appDrawer">
<ul id="topIcons">
<li>
<button type="button" className="navIcon" onClick={toggleMenu}>
<FontAwesomeIcon icon={faBars} />
</button>
</li>
<li>
<button type="button" className="navIcon">
<FontAwesomeIcon icon={faPlus} />
</button>
</li>
</ul>
<nav>
<ul id="menuList">
<Link href="/">
<a>
<p>Home</p>
</a>
</Link>
<Link href="/">
<a>Favorites</a>
</Link>
<Link href="/">
<a>Notifications</a>
</Link>
<Link href="/">
<a>Login/Sign up</a>
</Link>
</ul>
</nav>
</div>
<style jsx>{`
#navContainer {
background-color: #504761;
color: white;
position: fixed;
width: 100%;
}
#navContainer a {
text-decoration: none;
color: white;
}
#mobileNav {
display: ${navBarStatus ? 'none' : ''};
}
#navBarTop {
color: white;
display: flex;
justify-content: space-between;
list-style: none;
padding: 1rem 1rem 0.5rem 1rem;
font-size: 1.5rem;
}
.navIcon {
color: white;
border: none;
background-color: #504761;
font-size: 1.5rem;
}
#navbarBottom {
display: flex;
list-style: none;
justify-content: space-evenly;
padding: 1rem 0;
}
#appDrawer {
display: ${navBarStatus ? 'block' : 'none'};
background-color: #504761;
height: 100vh;
}
#topIcons {
display: flex;
justify-content: space-between;
list-style: none;
padding: 1rem;
font-size: 1.5rem;
}
#menuList {
padding: 2rem 0 2rem 1rem;
display: flex;
flex-direction: column;
}
#menuList a {
font-size: 2rem;
margin: 1rem 0;
}
`}</style>
</nav>
)
}
export default NavBar

Resources