【问题标题】:I made a voteapp with react.js, used styled-components but in index.html rendering all我用 react.js 制作了一个 voteapp,使用了 styled-components 但在 index.html 中渲染了所有
【发布时间】:2019-02-01 09:17:30
【问题描述】:

使用预定组件设计时发生错误该错误是由重叠组件引起的。我使用路由设置了 url。出来的第一个组件是app组件,但是VoteComponent出来了。有什么问题?我不认为这就是为什么它是由 url 引起的。 this is my github

组件/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>
                    &nbsp;&nbsp;&nbsp;
                    <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%);
`

设置/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>
    )
}

【问题讨论】:

    标签: reactjs react-router react-redux axios styled-components


    【解决方案1】:

    您依赖的 URL 很长,而这 - 很可能 - 不会发生。 zxc 变量可能被分配了一个空字符串值,使您的 Vote 路由到根路径,因为 /${""} 等于 /。

    【讨论】:

      猜你喜欢
      • 2021-09-22
      • 2022-01-02
      • 2021-08-31
      • 2020-08-21
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      相关资源
      最近更新 更多