简体   繁体   English

使用带有Href的ReactModal按钮,不起作用。 不确定为什么

[英]Using ReactModal button with Href, not working. Unsure why

I am fairly new to react and have redone my personal site in react. 我是新来的反应者,并已在反应中重做我的个人网站。 The issue I am running into is my button that links (with href) to my JSfiddle for each portfolio demo is not working. 我遇到的问题是我的按钮无法链接(带有href)到我的JSfiddle,因为每个投资组合演示均不起作用。 I am not sure if I did not bind correctly or what exactly the issue is other than when the modal is open, the Demo button does not work. 我不确定我是否没有正确绑定,或者除了打开模态时问题到底出在什么地方,“演示”按钮不起作用。 Close modal button works fine. 关闭模式按钮可以正常工作。 Please see code below. 请参见下面的代码。

import React from 'react';
import ReactModal from 'react-modal';

class Project extends React.Component {
constructor () {
    super();
        this.state = {
          showModal: false
    };

    this.handleOpenModal = this.handleOpenModal.bind(this);
    this.handleCloseModal = this.handleCloseModal.bind(this);
  }

handleOpenModal() {
    this.setState({ showModal: true});
}

handleCloseModal() {
    this.setState({ showModal: false});
}

componentWillMount() {
    ReactModal.setAppElement('body');
}

render() {
    const { details } = this.props;

    return (
        <li className="Project">
            <div onClick={this.handleOpenModal}>
                <img className="Project-image" src={'projects/' + details.image} alt={details.name}/>
                <div className="Project-overlay">
                    <p>{details.name}</p>
                </div>
            </div>
            <div >
                <ReactModal 
                    isOpen={this.state.showModal} 
                    contentLabel="This is my Modal" 
                    shouldCloseOnOverlayClick={true}
                    onRequestClose={this.handleCloseModal}
                >
                  <div className="modal-header">
                      <h3>{details.name}</h3>
                  </div>
                  <div className="modal-body">
                      <img className="Project-image" src={'projects/' + details.image} alt={details.name} />
                      <p className="desc-body">{details.desc}</p>
                      <p className="desc-body">{details.link}</p>
                  </div>
                  <div className="modal-footer">
                      { details.havLink && <button className="button" href={details.link}>Click for Demo!</button> }
                      <button className="button" onClick={this.handleCloseModal}>Close Modal</button>
                  </div>
                </ReactModal>

            </div>
            <div className="Project-tag">
                <p>{details.tag}</p>
            </div>
        </li>
    )
  }
}

const props = {};

export default Project;

The issue is in the first line of the "modal-footer" class. 问题在“模态页脚”类的第一行。 This button will show if the havLink property is true. 如果havLink属性为true,则此按钮将显示。 This data is being exported from another JS file. 此数据正在从另一个JS文件导出。 Everything else (image, description, modal title) all import correctly, even the link I set imports correctly but when the button is pushed nothing fires as I expected. 其他所有内容(图像,描述,模式标题)都可以正确导入,即使我设置的链接也可以正确导入,但是当按下按钮时,不会触发任何异常。 I do not see any errors in my React dev tools either. 我的React开发工具中也没有看到任何错误。

{details.link} as an href is not routing me to the specified link. {details.link}作为href并没有将我路由到指定的链接。 The link will show up in the paragraph tag though (just to see if correct link populated). 该链接将显示在段落标记中(只是为了查看是否填充了正确的链接)。

Let me know if anything else is needed, I am hoping the solution is as simple as an incorrect binding. 让我知道是否还有其他需要,我希望解决方案就像不正确的绑定一样简单。 Thank you in advance! 先感谢您!

<button> does not have the href attribute. <button>没有href属性。 You should be using an anchor element <a> . 您应该使用锚元素<a> To the anchor you can pass whatever class or style you want to make it look like a button, but it's still an anchor element, not button. 对于锚,您可以传递想要使其看起来像按钮的任何classstyle ,但它仍然是锚元素,而不是按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM