How do I use NavHashLink to smooth scroll through my pages? - reactjs

I want to have all my pages on one page that i could scroll through and when i press the button on the navbar, it scrolls to that section.
In my App JS I have the following normal setup
<Switch>
<Router>
<Sidebar />
<Route path='/Home' component={Home} />
<Route path='/Education' component={Education} />
<Route path='/Skills' component={Skills} />
<Route path='/Experience' component={Experience} />
<Route path='/Projects' component={Projects} />
</Switch>
</Router>
I am using as my navigation, inside sidebar we have
import React, { useState } from "react";
import { NavHashLink } from "react-router-hash-link";
const Container = styled.div`
position: fixed;
z-index: 100;
top: 50%;
transform: translateY(-50%);
.active {
border-right: 4px solid var(--white);
border-radius: 0 30px 30px 0;
img {
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(93deg)
brightness(103%) contrast(103%);
}
}
`;
const SlickBar = styled.ul`
color: var(--white);
list-style: none;
display: flex;
flex-direction: column;
align-items: center;
background-color: var(--black);
top: 10rem;
left: 0;
width: ${(props) => (props.clicked ? "14rem" : "3.5rem")};
transition: all 0.5s ease;
border-radius: 0 30px 30px 0;
`;
const Item = styled(NavHashLink)`
text-decoration: none;
color: var(--white);
width: 100%;
padding: 1rem 0;
cursor: pointer;
display: flex;
padding-left: 1rem;
&:hover {
border-right: 4px solid var(--white);
border-radius: 0 30px 30px 0;
img {
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(93deg)
brightness(103%) contrast(103%);
}
}
img {
width: 1.2rem;
height: auto;
filter: invert(92%) sepia(4%) saturate(1033%) hue-rotate(169deg)
brightness(78%) contrast(85%);
}
`;
const Text = styled.span`
width: ${(props) => (props.clicked ? "100%" : "0")};
overflow: hidden;
margin-left: ${(props) => (props.clicked ? "1.5rem" : "0")};
transition: all 0.3s ease;
`;
const Sidebar = () => {
const [click, setClick] = useState(false);
return (
<Container>
<SlickBar
onMouseEnter={() => setClick(true)}
onMouseLeave={() => setClick(false)}
clicked={click}>
<Item
onClick={() => setClick(false)}
exact
activeClassName="active"
to="/Home"
>
<img src={Home} alt="Home" />
<Text clicked={click}>DashBoard</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Education"
>
<img src={Education} alt="Education" />
<Text clicked={click}>Education</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Skills"
>
<img src={Skills} alt="Skills" />
<Text clicked={click}>Skills</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Experience"
>
<img src={Experience} alt="Experience" />
<Text clicked={click}>Experience</Text>
</Item>
<Item
onClick={() => setClick(false)}
activeClassName="active"
to="/Projects"
>
<img src={Projects} alt="Projects" />
<Text clicked={click}>Projects</Text>
</Item>
</SlickBar>
</Container>
);
};
After watching many tutorial ands trying different things nothing seemed to work.

Related

React Router V6 - Opening a Modal with the parent element loaded in the background

I want to make an https://unsplash.com/ clone and I'm stuck on the modal part.
I want when clicking an image, a modal pop out and display the content with the gallery in the background as well as the url to change .
This is how my code looks like right now:
<Navbar />
<Routes >
<Route path='/' element={<Home />}>
<Route index element={<ImagesContainer />}></Route>
</Route>
<Route path='/videos' element={<VideoContainer />}></Route>
<Route path='/:type/:id' element={<ImagePopup />} />
</Routes>
.modal_overlay{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2;
background-color: rgba(0, 0, 0, 0.5);
}
.modal_container{
position: relative;
max-width: 600px;
min-width: 450px;
width: 50vw;
height: 80vh;
background-color: #404752;
border-radius: 15px;
box-shadow: 0px 0px 18px 0px rgba(0, 0, 0, 0.75);
overflow: hidden;
}
const Modal = ( props:{closeFunction:() => void, children?: React.ReactNode) => {
return (
<div className={styles.modal_overlay} onClick={props.closeFunction}>
<div className={styles.modal_container} onClick={(e)=>{e.stopPropagation();}} style={props.style}>
<div className={styles.modal_content} style={props.contentStyle}>
{props.children}
</div>
</div>
</div>
)
}
Open or close it with an useState
I get somenthing like this:
My Modal

I’m building a portfolio but the sidebar component is not showing on any of the pages?

The sidebar is not showing on the home page of my portfolio or on any other pages like it’s supposed to be. Im wondering if it’s the CSS or something deeper. I assume the relevant code to this issue is of course the Sidebar component, App.js, and maybe Layout component and the scss file for the sidebar. Does anyone have any advice on how to narrow down as to which few files are likely the problem as it feels like it could be any of the components or their scss is not working together. Anyone know how to fix the sidebar not showing?
Sidebare index.js --->
import './index.scss'
import LogoS from '../../assets/images/logo-s.png'
import LogoSubtitle from '../../assets/images/logo_sub.png'
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import {
faLinkedin,
faGithub,
faYoutube,
faSkype,
} from '#fortawesome/free-brands-svg-icons'
import { faHome, faUser, faEnvelope, faSuitcase } from '#fortawesome/free-solid-svg-icons'
import { Link, NavLink } from 'react-router-dom'
const Sidebar = () => {
return (
<div className="nav-bar">
<Link className="logo" to="/">
<img src={LogoS} alt="Logo" />
<img className="sub-logo" src={LogoSubtitle} alt="slobodan" />
</Link>
<nav>
<NavLink exact="true" activeclassname="active" to="/">
<FontAwesomeIcon icon={faHome} color="#4d4d4e" />
</NavLink>
<NavLink activeclassname="active" className="about-link" to="/about">
<FontAwesomeIcon icon={faUser} color="#4d4d4e" />
</NavLink>
<NavLink activeclassname="active" className="portfolio-link" to="/portfolio">
<FontAwesomeIcon icon={faSuitcase} color="#4d4d4e" />
</NavLink>
<NavLink
activeclassname="active"
className="contact-link"
to="/contact"
>
<FontAwesomeIcon icon={faEnvelope} color="#4d4d4e" />
</NavLink>
</nav>
<ul>
<li>
<a
href="https://www.linkedin.com/in/slobodan-gaji%C4%87-006bb8b8/"
target="_blank"
rel="noreferrer"
>
<FontAwesomeIcon icon={faLinkedin} color="#4d4d4e" />
</a>
</li>
<li>
<a
href="https://github.com/bobangajicsm"
target="_blank"
rel="noreferrer"
>
<FontAwesomeIcon icon={faGithub} color="#4d4d4e" />
</a>
</li>
<li>
<a
href="https://www.youtube.com/channel/UCBu5ulO4d-d47lAVybpRTkw"
rel="noreferrer"
target="_blank"
>
<FontAwesomeIcon icon={faYoutube} color="#4d4d4e" />
</a>
</li>
<li>
<a href="skype:live:bobangajicsm" rel="noreferrer" target="_blank">
<FontAwesomeIcon icon={faSkype} color="#4d4d4e" />
</a>
</li>
</ul>
</div>
)
}
export default Sidebar
Sidebare scss -->
.nav-bar {
background: #181818;
width: 60px;
height: 100%;
position: absolute;
top: 0;
z-index: 3;
min-height: 500px;
.logo {
display: block;
padding: 8px 0;
img {
display: block;
margin: 8px auto;
width: 24px;
height: auto;
&.sub-logo {
width: 50px;
}
}
}
nav {
display: block;
text-align: center;
position: absolute;
height: 210px;
top: 50%;
margin-top: -120px;
width: 100%;
a {
font-size: 22px;
color: #4d4d4e;
display: block;
line-height: 51px;
height: 51px;
position: relative;
text-decoration: none;
i {
transition: all 0.3s ease-out;
}
&:hover {
color: #ffd700;
svg {
opacity: 0;
}
&:after {
opacity: 1;
}
}
&:after {
content: '';
font-size: 9px;
letter-spacing: 2px;
position: absolute;
bottom: 0;
display: block;
width: 100%;
text-align: center;
opacity: 0;
-webkit-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
&:first-child {
&:after {
content: 'HOME';
}
}
}
a.about-link {
&:after {
content: 'ABOUT';
}
}
a.contact-link {
&:after {
content: 'CONTACT';
}
}
a.portfolio-link {
&:after {
content: 'PORTFOLIO';
}
}
a.active {
svg {
color: #ffd700;
}
}
}
ul {
position: absolute;
bottom: 20px;
width: 100%;
display: block;
padding: 0;
list-style: none;
text-align: center;
margin: 0;
li {
a {
padding: 7px 0;
display: block;
font-size: 15px;
line-height: 16px;
color: #4d4d4e;
&:hover {
color: #ffd700;
}
}
}
}
}
App.js --->
import { Route, Routes } from 'react-router-dom'
import Home from './components/Home'
import About from './components/About'
import Contact from './components/Contact'
import Layout from './components/Layout'
import Portfolio from './components/Portfolio'
import Dashboard from './components/Dashboard'
import './App.scss'
function App() {
return (
<>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="about" element={<About />} />
<Route path="/contact" element={<Contact />} />
<Route path="/portfolio" element={<Portfolio />} />
<Route path="/dashboard" element={<Dashboard />} />
</Route>
</Routes>
</>
)
}
export default App
Layout index.js -->
import { Outlet } from 'react-router-dom'
import Sidebar from '../Sidebar/'
import './index.scss'
const Layout = () => {
return (
<div className="App">
<Sidebar />
<div className="page">
<span className="tags top-tags"><body></span>
<Outlet />
<span className="tags bottom-tags">
</body>
<br />
<span className="bottom-tag-html"></html></span>
</span>
</div>
</div>
)
}
export default Layout
Thank you in advance any help is greatly appreciated!!

Close mobile navigation menu after anchor link click - React

I am new to React and am trying to create a mobile menu that I somehow created using several online resources. Now the problem is that my mobile menu is not closing after clicking on the Anchor links.
I have three different files (Burger, Navbar, and RightNav). Burger is just for icon transformation, Navbar is for main navigation, and RightNav is the mobile menu.
Can someone please specify how I can close the RightNav after clicking on any menu anchor link?
Burger.jsx file
import React, { useState } from 'react';
import styled from 'styled-components';
import RightNav from './RightNav';
const StyledBurger = styled.div`
width: 2rem;
height: 2rem;
position: fixed;
align-items:center;
right: 20px;
z-index: 20;
display: none;
top: 5%;
#media (max-width: 1000px) {
display: flex;
justify-content: space-around;
flex-flow: column nowrap;
align-items:flex-start;
}
div {
width: 2rem;
height: 0.25rem;
background-color: ${({ open }) => open ? '#00a993' : '#00a993'};
border-radius: 10px;
transform-origin: 1px;
transition: all 0.3s linear;
&:nth-child(1) {
transform: ${({ open }) => open ? 'rotate(45deg)' : 'rotate(0)' };
}
&:nth-child(2) {
transform: ${({ open }) => open ? 'translateX(100%)' : 'translateX(0)'};
opacity: ${({ open }) => open ? 0 : 1};
}
&:nth-child(3) {
transform: ${({ open }) => open ? 'rotate(-45deg)' : 'rotate(0)'};
width: ${({ open }) => open ? '20px':'16px'};
}
}
`;
const Burger = () => {
const [open, setOpen] = useState(false)
return (
<>
<StyledBurger open={open} onClick={() => setOpen(!open)} id="burger" className='burger'>
<div />
<div />
<div />
</StyledBurger>
<RightNav open={open}/>
</>
)
}
export default Burger
RightNav.jsx file
import React from 'react';
import styled from 'styled-components';
import { NavHashLink } from 'react-router-hash-link';
const Ul = styled.ul`
list-style: none;
display: flex;
flex-flow: row nowrap;
li {
padding: 18px 10px;
}
#media (max-width: 1000px) {
z-index: 1;
flex-flow: column nowrap;
background-color: #13161B;
position: fixed;
transform: ${({ open }) => open ? 'translateX(0)' : 'translateX(100%)'};
top: 10%;
right: 0;
width: 100%;
height:auto;
padding: 1.5rem;
align-items: center;
transition: transform 0.3s ease-in-out;
font-size: 16px;
font-family: 'Quicksand', sans-serif;
text-transform: uppercase;
font-weight: 600;
line-height: 7rem;
}
`;
const RightNav = ({ open, setOpen }) => {
return (
<Ul open={open} className="mobile-menu" id="mobile-menu">
<NavHashLink smooth to="/#welcome-section" activeClassName="selected" style={{ color: '#00a993' }}>
What
</NavHashLink>
<NavHashLink smooth to="/#how-section" activeClassName="selected" style={{ color: '#00a993' }}>
How
</NavHashLink>
<NavHashLink smooth to="/#IsItAThing-section" activeClassName="selected" style={{ color: '#00a993' }}>
Why
</NavHashLink>
<NavHashLink smooth to="/#team-section" activeClassName="selected" style={{ color: '#00a993' }}>
Who
</NavHashLink>
<NavHashLink smooth to="/#contact-section" activeClassName="selected" style={{ color: '#00a993' }}>
Where
</NavHashLink>
<a href='https://stories.therelevancehouse.com/' style={{ color: '#00a993' }}>
Stories
</a>
</Ul>
)
}
export default RightNav
Navbar.jsx file
import styled from 'styled-components';
import Burger from './Burger';
import React, { useEffect, useState } from 'react';
import '../App.css';
import logo from '../images/logomain.png'
import ln from '../images/LinkedIN_white.svg'
import medium from '../images/Medium_white.svg'
import twitter from '../images/Twitter_white.svg'
import fb from '../images/Facebook_white.svg'
import instagram from '../images/Instagram_white.svg'
import '../fonts/norwester.ttf'
import linkedinhover from '../images/LinkedinIconHover.svg'
import facebookhover from '../images/FacebookIconHover.svg'
import twitterhover from '../images/TwitterIconHover.svg'
import instagramhover from '../images/InstagramIconHover.svg'
import mediumhover from '../images/MediumIconHover.svg'
import { NavHashLink } from 'react-router-hash-link'
const Nav = styled.nav`
width: 100%;
border-bottom: 2px solid #f1f1f1;
padding: 20px 20px;
display: flex;
justify-content: space-between;
align-items: center;
.logo {
padding: 15px 0;
}
`
const Navbar = () => {
const [show, setShow] = useState(true);
const [lastScrollY, setLastScrollY] = useState(0);
useEffect(() => {
const controlNavbar = () => {
if (typeof window !== 'undefined') {
if ((window.scrollY > lastScrollY) && (window.scrollY > 200) && (window.innerWidth > 1000))
{
// document.getElementById("navbar").style.padding = "30px 10px";
document.getElementById("navtext").style.fontSize = "12px";
document.getElementById("navtext1").style.fontSize = "12px";
document.getElementById("navtext2").style.fontSize = "12px";
document.getElementById("navtext3").style.fontSize = "12px";
document.getElementById("navtext4").style.fontSize = "12px";
document.getElementById("navtext5").style.fontSize = "12px";
document.getElementById("nav-bar-logo1").style.height = "80px";
document.getElementById("nav").style.height = "90px";
// setShow(false);
} else {
setShow(true);
document.getElementById("navtext").style.fontSize = "14px";
document.getElementById("navtext1").style.fontSize = "14px";
document.getElementById("navtext2").style.fontSize = "14px";
document.getElementById("navtext3").style.fontSize = "14px";
document.getElementById("navtext4").style.fontSize = "14px";
document.getElementById("navtext5").style.fontSize = "14px";
document.getElementById("nav-bar-logo1").style.height = "105px";
document.getElementById("nav").style.height = "120px";
}
setLastScrollY(window.scrollY);
}
};
if (typeof window !== 'undefined') {
window.addEventListener('scroll', controlNavbar);
return () => {
window.removeEventListener('scroll', controlNavbar);
};
}
}, [lastScrollY]);
return (
<Nav >
<nav id='nav' className={ `${show && 'nav'}`} >
<a href="./" >
<img id='nav-bar-logo1' className='nav-bar-logo' alt="Marketing, Branding and Communications Agency"
src={logo} />
</a>
<div id='navigation-social'>
<a href='/'>
<img className='social-nav' src={ln} alt='linkedin' onMouseOver={e => e.currentTarget.src = linkedinhover} onMouseOut={e => e.currentTarget.src = ln} />
</a>
<a href='/'>
<img className='social-nav' src={medium} alt='medium' onMouseOver={e => e.currentTarget.src = mediumhover} onMouseOut={e => e.currentTarget.src = medium} />
</a>
<a href='/'>
<img className='social-nav' src={twitter} alt='twitter' onMouseOver={e => e.currentTarget.src = twitterhover} onMouseOut={e => e.currentTarget.src = twitter} />
</a>
<a href='/'>
<img className='social-nav' src={fb} alt='facebook' onMouseOver={e => e.currentTarget.src = facebookhover} onMouseOut={e => e.currentTarget.src = fb} />
</a>
<a href='/'>
<img className='social-nav' src={instagram} alt='instagram' onMouseOver={e => e.currentTarget.src = instagramhover} onMouseOut={e => e.currentTarget.src = instagram} />
</a>
</div>
<div id='navigation-items'>
<NavHashLink id="navtext" smooth to="/#welcome-section" activeClassName="selected" activeStyle={{ color: '#00a993' }}>
What
</NavHashLink>
<NavHashLink id="navtext1" smooth to="/#how-section" activeClassName="selected" activeStyle={{ color: '#00a993' }}>
How
</NavHashLink>
<NavHashLink id="navtext2" smooth to="/#IsItAThing-section" activeClassName="selected" activeStyle={{ color: '#00a993' }}>
Why
</NavHashLink>
<NavHashLink id="navtext3" smooth to="/#team-section" activeClassName="selected" activeStyle={{ color: '#00a993' }}>
Who
</NavHashLink>
<NavHashLink id="navtext4" smooth to="/#contact-section" activeClassName="selected" activeStyle={{ color: '#00a993' }}>
Where
</NavHashLink>
<a id="navtext5" href='https://stories.therelevancehouse.com/'>
Stories
</a>
</div>
</nav>
<Burger />
</Nav>
)
}
export default Navbar

Objects are not valid as a React child. If you meant to render a collection of children, use an array instead. React

Full error: Error: Objects are not valid as a React child (found: object with keys {seconds, nanoseconds}). If you meant to render a collection of children, use an array instead.
Code:
...
const docRef = collection(db, 'posts')
const [docSnapshot] = useCollection(docRef)
...
{docSnapshot?.docs?.map((feed) => (
<Feed
key={feed.data().id}
navigation={navigation}
username={feed.data().username}
theme={feed.data().theme}
body={feed.data().body}
imagesURL={feed.data().imagesURL}
photoURL={feed.data().photoURL}
created_at={feed.data().created_at}
comments={feed.data().comments}
likes={feed.data().likes}
shares={feed.data().shares}
/>
))}
...
Firebase:
firebase
Feed:
import React from 'react'
import { View } from 'react-native'
import styled from 'styled-components/native'
import {
Entypo,
AntDesign,
MaterialCommunityIcons
} from '#expo/vector-icons'
import { Avatar, Image } from 'react-native-elements'
import { TouchableOpacity } from 'react-native-gesture-handler'
const Container = styled.View`
flex: 1;
`
const Header = styled.View`
height: 50px;
flex-direction: row;
align-items: center;
justify-content: space-between;
margin-top: 6px;
padding: 0 11px;
`
const Row = styled.View`
align-items: center;
flex-direction: row;
`
const User = styled.Text`
font-size: 12px;
font-weight: bold;
color: #222121;
`
const Time = styled.Text`
font-size: 9px;
color: #747476;
`
const Post = styled.Text`
font-size: 14px;
color: #222121;
line-height: 14px;
margin-top: 15px;
padding: 0 11px;
`
const Photo = styled.Image`
width: 100%;
height: 300px;
`
const Footer = styled.View`
padding: 0 11px;
`
const FooterCount = styled.View`
flex-direction: row;
justify-content: space-between;
padding: 9px 0;
`
const IconCount = styled.View`
background: #1878f3;
width: 20px;
height: 20px;
border-radius: 10px;
align-items: center;
justify-content: center;
margin-right: 6px;
`
const TextCount = styled.Text`
font-size: 11px;
color: #424040;
`
const Separator = styled.View`
width: 100%;
height: 1px;
background: #f9f9f9;
`
const FooterMenu = styled.View`
flex-direction: row;
justify-content: space-between;
padding: 9px 0;
`
const Button = styled.TouchableOpacity`
flex-direction: row;
`
const Icon = styled.View`
margin-right: 6px;
`
const Text = styled.Text`
font-size: 12px;
color: black;
`
const BottomDivider = styled.View`
width: 100%;
height: 9px;
background: #f0f2f5;
`
const addLike = () => {
}
const addShare = () => {
}
const Feed = ({ navigation, theme, username, body, comments, shares, likes, imagesURL, photoURL, created_at }) => {
return (
<>
<Container>
<Header>
<Row>
<TouchableOpacity onPress={() => navigation.navigate('UserScreen', {
userName: username
})}>
<Avatar
rounded
source={{
uri: photoURL
}}
/>
</TouchableOpacity>
<View style={{paddingLeft: 10}}>
<TouchableOpacity onPress={() => navigation.navigate('ThemeScreen', {
theme: theme
})}>
<User>{theme}</User>
</TouchableOpacity>
<Row>
<Text>{created_at}</Text>
<Entypo
name='dot-single'
size={18}
color='#747476'
/>
<TouchableOpacity onPress={() => navigation.navigate('UserScreen', {
userName: username
})}>
<Text>Posted by {username}</Text>
</TouchableOpacity>
</Row>
</View>
</Row>
<TouchableOpacity onPress={() => console.log('Dots')}>
<Entypo
name='dots-three-horizontal'
size={15}
color='#222121'
/>
</TouchableOpacity>
</Header>
<Post>{body}</Post>
{imagesURL?.map((imageURL) => (
<Photo resizeMode='contain' source={{uri: imageURL}} key={imageURL} />
))}
<Footer>
<Separator />
<FooterMenu>
<Button>
<Icon>
<AntDesign
name='like2'
size={20}
color='#424040'
/>
</Icon>
<TouchableOpacity onPress={() => addLike()}>
<Text>{likes} Likes</Text>
</TouchableOpacity>
</Button>
<Button>
<Icon>
<MaterialCommunityIcons
name='comment-outline'
size={20}
color='#424040'
/>
</Icon>
<TouchableOpacity onPress={() => navigation.navigate('PostScreen', {
username: username,
theme: theme,
body: body,
comments: comments,
photoURL: photoURL,
imagesURL: imagesURL,
shares: shares,
likes: likes,
created_at: created_at
})}>
<Text>{comments} Comments</Text>
</TouchableOpacity>
</Button>
<Button>
<Icon>
<MaterialCommunityIcons
name='share-outline'
size={20}
color='#424040'
/>
</Icon>
<TouchableOpacity onPress={() => addShare()}>
<Text>{shares} Shares</Text>
</TouchableOpacity>
</Button>
</FooterMenu>
</Footer>
<BottomDivider />
</Container>
</>
)
}
export default Feed
With console log all is working
I really have no idea
i don't know what to write i just need to be able to post this questions because there is too much code so im writing this pointless text
<Text>{created_at}</Text>
created_at is an object (specifically, a Timestamp), so you can't put it as a child. It has various functions that you can use to manipulate it, so for example you could do:
<Text>{created_at.seconds}</Text>
or
<Text>{created_at.toString()}</Text>
or
<Text>{created_at.toDate().toLocaleTimeString()}</Text>
There's many things you could do, it just depends what you want to show. But you can't show the whole object.

I made a voteapp with react.js, used styled-components but in index.html rendering all

An error occurred while designing with scheduled-components The error is caused by overlapping components. I set up the url using route. The first component that comes out is the app component, but the VoteComponent comes out. What's the problem? I don't think that's why it's caused by url. this is my github
components/App.js
injectGlobal`
html, body{
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#root{
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
`
render(){
return(
<Container1>
<Poll>
<Header>
<Logo>
투표
</Logo>
</Header>
<form onSubmit={this.handleSubmit} style={formStyle}>
<div style={{ width: '60%', height:'50%', marginLeft: 'auto', marginRight: 'auto'}}>
<input type={'text'} placeholder={'질문을 입력하세요'} style={inputStyle} name={'ques'}
onChange={this.handleChange} />
<input type={'text'} placeholder={'선택할항목을 입력하세요'} style={inputStyle} name={'ans1'}
onChange={this.handleChange}/>
<input type={'text'} placeholder={'선택할항목을 입력하세요'} style={inputStyle} name={'ans2'}
onChange={this.handleChange}/>
<input type={'text'} placeholder={'접근할 url 5자리숫자로 정해주세요'} style={inputStyle} name={'url'} onChange={this.handleChange} maxLength={'5'}/>
</div>
<div style={{ width: '60%', height:'30%', marginLeft: 'auto', marginRight: 'auto'}}>
<input type={'submit'} value={'질문페이지 만들기'}/>
<br/>
<Link to={`/${this.state.url}`}>
페이지가기
</Link>
</div>
</form>
</Poll>
</Container1>
)
}
const Container1 = styled.div`
width: 100%;
height: 100%;
background-color:#ffffff;
`
const Header = styled.div`
width: 50%;
height: 20%
margin-left: auto;
margin-right: auto;
text-align: center;
font-size: 250%
`
const Logo = styled.div`
width:100%;
height: 100%;
`
const Poll = styled.div`
width: 50%;
height: 50%;
background-color: skyblue;
left: 50%;
top: 50%;
position: absolute;
transform: translate(-50%,-50%);
`
let formStyle = {
width:'100%',
height:'100%'
}
let inputStyle ={
width:'100%',
height:'20%',
}
components/Vote.js
render() {
let ques = this.state.ques;
let ans1 = this.state.ans1;
let ans2 = this.state.ans2;
let asd = document.URL;
let zxc = asd.substring(28);
return (
<Container2>
<Poll2>
<div style={{width: '100%', height:'80%'}}>
<div style={{marginBottom: '4%', textAlign: 'center', fontSize:'250%'}}>{ques}</div>
<div style={{marginBottom: '3%', textAlign: 'center', fontSize:'150%'}}>
<input type={'radio'} value={ans1} name={'option'} />
<span>{ans1}</span>
<input type={'radio'} value={ans2} name={'option'}/>
<span>{ans2}</span>
</div>
<div style={{textAlign: 'center', fontSize:'150%', width:'100%', height:'30%'}}>
<input type={'submit'} value={'투표하기'} onClick={this.handleClick} style={{width:'30%', height:'50%'}}/>
<Link to={`/${zxc}/result`}><input type={'button'} value={'결과창가기'} style={{width:'30%', height:'100%'}}/></Link>
</div>
</div>
<div style={{width: '100%', height:'20%'}}>
<button onClick={this.refresh} style={{width:'100%', height:'100%', fontSize:'100%', border:'none', backgroundColor:'#f1f2f4'}}>새로고침</button>
</div>
</Poll2>
</Container2>
)
}
const Container2 = styled.div`
width:100%;
height:100%;
`
const Poll2 = styled.div`
width: 50%;
height: 50%;
background-color: skyblue;
left: 50%;
top: 50%;
position: absolute;
transform: translate(-50%,-50%);
`
setting/Root.js
render(){
let asd = document.URL;
let zxc = asd.substring(28, 33);
return(
<Fragment>
<Router>
<Fragment>
<Route path={'/'} exact={true} component={App}/>
<Route path={`/${zxc}`} component={Vote}/>
<Route path={`/${zxc}/result`} component={Result}/>
</Fragment>
</Router>
</Fragment>
)
}
You're relying on the URL being quite long and this - most likely - doesn't happen. The zxc variable is probably assigned an empty string value making your Vote route into a root path, since /${""} equals just /.

Resources