hello I am trying to start a beginner react project using react and styled components. I was following a tutorial that showed me how to import some stock react logos, but I can't figure out how to do it using my own image.
export const NavbarContainer = styled(Container)`
display: flex;
justify-content: space-between;
height: 80px;
${Container}
`;
export const NavLogo = styled(Link)`
color: #fff;
justify-self: flex-start;
cursor: pointer;
text-decoration: none;
font-size: 2rem;
display: flex;
align-items: center;
`
export const NavIcon = styled(Icon)`
margin-right: 0.5rem;
`
Below is the code for my navigation bar
import React from 'react';
import { Nav, NavbarContainer, NavLogo, NavIcon } from './Navbar.elements';
import { ReactComponent as Icon } from './icon.png';
const Navbar = () => {
return (
<>
<Nav>
<NavbarContainer>
<NavLogo to="/">
<NavIcon />
SKYPRECISION
</NavLogo>
</NavbarContainer>
</Nav>
</>
)
}
export default Navbar
You need to add an img tag like this
<img src={src} alt="My awesome logo" />
You can replace the NavIcon with that.
If you want that image to utilize styled-components you can do something like:
const MyImage = styled.img`
your css rules here
`
and use it in code
<NavLogo to="/">
<MyImage />
SKYPRECISION
</NavLogo>
Related
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.
I'm playing around with styled-components and having troubles working with material-ui. So thats the code:
import React from "react";
import styled from "styled-components";
import ChatIcon from "#mui/icons-material/Chat";
import MoreVertIcon from "#mui/icons-material/MoreVert";
import { Avatar} from "#mui/material";
function Sidebar() {
return (
<Container>
<Header>
<UserAvatar className={"override"} />
<IconsContainer>
<IconButton>
<ChatIcon />
</IconButton>
<IconButton>
<MoreVertIcon />
</IconButton>
</IconsContainer>
</Header>
</Container>
);
}
export default Sidebar;
const Container = styled.div``;
const Header = styled.div`
display: flex;
position: sticky;
top: 0;
background-color: white;
z-index: 1;
justify-content: space-between;
align-items: center;
padding: 15px;
height: 80px;
border-bottom: 1px solid whitesmoke;
`;
const UserAvatar = styled(Avatar)`
height: 60px;
`;
const IconsContainer = styled.div`;
The styled div is working completely fine, but when I import 'Avatar' from #mui and try to add some styling named UserAvatar, it is completely ignoring what I'm writing down.
I think that with mui they have their own version of styled for styled components. https://mui.com/system/styled/
It appears they want you to use it rather than styled-components
import Button from '#mui/material/Button';
import { styled } from '#mui/material/styles';
const CustomButton = styled(Button)({
// your custom styles go here
}) as typeof Button;
see the import from import { styled } from '#mui/material/styles';;
I'm relatively new to reactjs and I'm trying to essentially make a simple "card game" where two cards go against each other and the higher score wins/stays, and the lower score loses, then the next card comes up against the initial winner. I have made a component that can be changed, but need help implementing it. Any help is appreciated or an example of how I can implement this.
import "./styles.css";
import React from "react";
import styled from "styled-components";
import data0 from "./NFTs/Team1/0.json";
import data1 from "./NFTs/Team1/1.json";
import data2 from "./NFTs/Team1/2.json";
import data3 from "./NFTs/Team1/3.json";
import data4 from "./NFTs/Team2/0.json";
import data5 from "./NFTs/Team2/1.json";
import data6 from "./NFTs/Team2/2.json";
import data7 from "./NFTs/Team2/3.json";
export default function App() {
//enter teams
var matchups = [
[data0, data1, data2, data3], //users 1
[data4, data5, data6, data7] //users 2
];
class Welcome extends React.Component {
render() {
return (
<div className="NFT">
<img
className={this.props.className}
src={this.props.src}
alt="NFT"
/>
<h1 className="nftnum">{this.props.name}</h1>
</div>
);
}
}
const NavBar = styled.div`
width: 100%;
height: 100px;
background: #fff;
position: fixed;
top: 0;
display: flex;
justify-content: space-between;
`;
const TeamName = styled.h1`
font-size: 30px;
display: flex;
flex-direction: column;
justify-content: center;
width: 50%;
`;
const Logo = styled.img`
position: fixed;
top: 0;
left: 50%;
transform: translate(-50%);
z-index: 1;
width: 25%;
`;
return (
<div className="App">
<Logo src="logo.png" />
<NavBar>
<TeamName>Aids Sack</TeamName>
<TeamName>Balls Tucked</TeamName>
</NavBar>
<div className="nftWrapper">
<Welcome
className="leftNFT"
name={matchups[0][0].name}
src={matchups[0][0].properties.files[0].uri}
/>
<Welcome
className="rightNFT"
name={matchups[1][0].name}
src={matchups[1][0].properties.files[0].uri}
/>
</div>
</div>
);
}
I'm trying to remove underline for my Link in react by using styled-components
if I use style={{textDecoration: "none"}} it will work but when I use text-decoration: none; in my styled components it won't work:
here is my js code:
import React from "react";
import styled from "styled-components";
import { Link } from "#reach/router";
import { CSSTransition, TransitionGroup } from "react-transition-group";
const StyleNavbarContainer = styled.nav`
background-color: #333333;
width: 100%;
color: #d6d6d6;
position: fixed;
text-align: center;
top: 0;
`;
const AppleLogo = styled.div`
font-size: 20px;
display: inline-block;
`;
const StyleLinkList = styled.ul`
display: inline-block;
`;
const StyledLink = styled(Link)`
padding: 12px 10px;
color: #d6d6d6;
text-decoration: none;
`;
const Navbar = (props) => {
return (
<>
<StyleNavbarContainer>
<AppleLogo>
<i class="fab fa-apple"></i>
</AppleLogo>
<StyleLinkList>
<StyledLink to="/">Mac</StyledLink>
</StyleLinkList>
</StyleNavbarContainer>
</>
);
};
export default Navbar;
add my App.jsx file here is the code:
import React from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import Navbar from "./components/Navbar";
function App() {
return (
<div className="App">
<Navbar />
</div>
);
}
export default App;
Need to remove import "bootstrap/dist/css/bootstrap.min.css"; from App.jsx
I have the following code:
// #flow
import React from 'react';
import { Route } from 'react-router-dom';
import Split from '../../components/grouping/Split';
import CenterBox from '../../components/grouping/CenterBox';
import styled from 'styled-components';
type Props = {
routes: Array<Object>,
background: String
};
export default ({ routes, background }: Props) =>
(<div>
<Split push="right" alignItems="center" height={50} pad={{ horizontal: 20 }}>
<div />
<div>This feature will be available soon!</div>
</Split>
<DashboardContent background={background}>
<CenterBox>
<div style={{ transform: 'translateY(50%)' }}>
<CenterBox height={300} width={500} backgroundColor="#FFFFFF">
This feature is not included as part of the design beta and will be available soon
</CenterBox>
</div>
</CenterBox>
</DashboardContent>
</div>);
const DashboardContent = styled.div`
background: url(${props => props.background}) no-repeat top
center;
background-size: 100%;
min-height: 100vh;
`;
I would like to pass background as a prop to my styled component so that when it is passed a link to an image file it sets it as a background, the code compiles but the background does not show up. What am I doing wrong?
Changing your background property to be a function that returns a string will fix your issue.
background: ${props => `url(${props.background}) no-repeat top center`};
Is this going to work out fine?
const DashboardContent = styled.div`
background: url(${props => props.background}) no-repeat top center;
`
<DashboardContent background={`https://xxxx/xxxx${background}`}/>
This worked for me!
import styled from 'styled-components';
import img from './img/background.gif';
const Content = styled.div`
background-image: url(${img});
`;