【问题标题】:Set state based on particular element根据特定元素设置状态
【发布时间】:2018-12-09 11:49:36
【问题描述】:

我有 3 个链接,我想在单击元素时更改链接的文本,但它会影响那里的所有链接,当我单击链接时,所有链接都会更改为 back,但我想要只有那个链接会改变,请问我该怎么做

 class Apps extends Component {
constructor(props) {
    super(props);
    // Don't do this!
    this.state = { showing: true, word: false };

}
render() {
    return (
        <div>
         <div className="container">
            <div style={{ display: (this.state.showing ? 'block' : 'none') }}>A Single Page web application
             made with react</div>
         </div>
            <div className="buttons">
                <a href='' ref="login" onClick={this.onclick.bind(this)} >{this.state.word ? 'back' : 'Login'}</a><br/><a href='' onClick={this.onclick.bind(this)}>{this.state.word ? 'back' : 'Signup'}</a><br />
                <a href='' onClick={this.onclick.bind(this)}>{this.state.word ? 'back' : 'Members'}</a>
            </div>

        </div>
    );

}onclick(e){
    e.preventDefault();
    this.setState({showing: !this.state.showing});
    this.setState({word: !this.state.word});
}
};

【问题讨论】:

    标签: javascript reactjs ecmascript-6 components state


    【解决方案1】:

    您可以将链接重构为具有自己状态的单独组件。

    【讨论】:

      猜你喜欢
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2021-11-25
      • 1970-01-01
      • 2017-04-22
      • 2017-07-18
      • 1970-01-01
      相关资源
      最近更新 更多