【问题标题】:React - use button for redirect linkReact - 使用按钮重定向链接
【发布时间】:2019-08-10 14:38:24
【问题描述】:

我是 React 的新手,如果这太基本了,请见谅。

我正在尝试在我的应用中添加一个按钮以将其重定向到 Spotify。

到目前为止,这就是我正在尝试的方式。

class Spotify extends Component {
  constructor () {
    super()
    this.handleClick = this.handleClick.bind(this)
  }

  handleClick () {
    console.log('Success!')
  }

  render () {
    return (
      <div className='button__container'>
        <button className='button' onClick={this.handleClick}>
          Link your Spotify account
        </button>
      </div>
    )
  }
}

export default Spotify;

现在,将上述按钮链接到:

 <a href="http://localhost:8888"></a>

【问题讨论】:

  • 您有什么特殊原因不能将您的 localhost:8888"></a> 添加到您的

标签: reactjs redirect href


【解决方案1】:

我认为您正在寻找这样的东西:

class Spotify extends Component {    
  render () {
    return (
      <div className='button__container'>
        <a className='button' role="button" href="http://someurl.com">
          Link your Spotify account
        </a>
      </div>
    )
  }
}

export default Spotify;

如果您的组件不需要状态,请考虑将上述代码重构为无状态组件,如下所示:

export const Spotify = () => (
      <div className='button__container'>
        <a className='button' role="button" href="http://someurl.com">
          Link your Spotify account
        </a>
      </div>
);

如果你只是想显示一个链接,锚标签就可以了。您可以将target="_blank" 添加到您的锚标记中,使其在新标签中打开。

【讨论】:

  • 正确!我对作者的问题添加了评论,因为我想了解是什么让他们认为他们不能继续使用锚点而不是按钮。
  • 实际上我需要状态,因为只有当用户尚未通过身份验证时,我才需要显示此按钮。考虑到状态,您能否更新您的答案?
  • 我的意思是保留onClick函数
猜你喜欢
  • 2020-10-07
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
相关资源
最近更新 更多