簡體   English   中英

從另一個組件打開模態點擊反應js

[英]Open modal from another component click in react js

我正在使用 react 的基本組件模態組件 - https://github.com/reactjs/react-modal

我想要實現的是我想從另一個導入了模態的父級打開模態。

Parent.js
<button onClick={() => this.refs.setState({modalIsOpen: true})}> - THIS BUTTON ELEMENT IS IN ANOTHER COMPONENT

Modal.js
import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-modal';

const customStyles = {
content : {
top                   : '50%',
left                  : '50%',
right                 : 'auto',
bottom                : 'auto',
marginRight           : '-50%',
transform             : 'translate(-50%, -50%)'
}
};

class App extends React.Component {
 constructor() {
 super();

 this.state = {
  modalIsOpen: false
 };

 this.openModal = this.openModal.bind(this);
 this.afterOpenModal = this.afterOpenModal.bind(this);
 this.closeModal = this.closeModal.bind(this);
}

openModal() {
 this.setState({modalIsOpen: true});
}

afterOpenModal() {
 // references are now sync'd and can be accessed.
 this.subtitle.style.color = '#f00';
 }

closeModal() {
 this.setState({modalIsOpen: false});
}

render() {
 return (
  <div>
    <button onClick={this.openModal}>Open Modal</button>
    <Modal
      isOpen={this.state.modalIsOpen}
      onAfterOpen={this.afterOpenModal}
      onRequestClose={this.closeModal}
      style={customStyles}
      contentLabel="Example Modal"
    >

      <h2 ref={subtitle => this.subtitle = subtitle}>Hello</h2>
      <button onClick={this.closeModal}>close</button>
      <div>I am a modal</div>
      <form>
        <input />
        <button>tab navigation</button>
        <button>stays</button>
        <button>inside</button>
        <button>the modal</button>
      </form>
    </Modal>
  </div>
);
}
}

export default App

我已經讀過這可以使用 refs 並更改模態的狀態來完成。 我到底做錯了什么?

謝謝!

您可以在父級中嘗試以下代碼嗎

<button onClick={() => this._modal.openModal()}>click</button>

當你調用你的模態組件時,使用ref屬性然后可以像上面的代碼一樣調用。

<Modal ref={(modal) => { this._modal = modal; }} />

簡單的方法,通過道具做到這一點:

模態.js

                import ....

                <Modal
                aria-labelledby="simple-modal-title"
                aria-describedby="simple-modal-description"
                className={classes.modal}
                open={this.props.handleOpen}
                onClose={this.props.handleClose}
                BackdropComponent={Backdrop}
                BackdropProps={{
                    timeout: 1000
                }}
            >

在導入了模態的組件中。

///some code here
state = {
        isOpen: Boolean(false)
    };
                <externalElement onClick={() => this.setState({ isOpen: true })}>title ... </externalElement>
                  <importedModal
                            handleOpen={this.state.isOpen}
                            handleClose={() => this.setState({ isOpen: false })}
                        />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM