【问题标题】:React Link to and If StatementReact Link to and If 语句
【发布时间】:2018-11-23 23:30:04
【问题描述】:

所以我有一个功能,我想在单击按钮时链接到项目的不同页面。 函数如下:

displayCharacterProfile() {
let getCharacterName = localStorage.getItem("characterName");

if(getCharacterName == "littlefinger") {
<Link to="/littlefinger"></Link>
}

if(getCharacterName == "tywin") {
<Link to="/tywin"></Link>
}
}


render() {
return (
<button className="enter" onClick={this.displayCharacterProfile}> Enter </button>
);
}

任何快速帮助将不胜感激!谢谢你:-)

【问题讨论】:

    标签: reactjs onclick local-storage link-to


    【解决方案1】:

    欢迎来到 StackOverflow!当你渲染一个Link 元素时,它所做的只是向用户显示一个链接。用户必须实际单击它才能到达那里。试试这个:

    render() {
      let getCharacterName = localStorage.getItem("characterName");
      return (
        <button className="enter">
          <Link to={"/" + getCharacterName}> Enter </Link>
        </button>
      );
    }
    

    【讨论】:

    • 最好将角色名称作为道具传递并让其他一些组件处理localStorage 调用,但我想这在技术上与您的问题无关:)
    • 非常感谢您快速准确的回复!它现在正在工作:-)
    • 太棒了!很高兴它正在工作。如果有帮助,请不要忘记接受并投票!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 2017-09-25
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多