简体   繁体   English

反应模态的奇怪行为

[英]Odd behavior with react-modal

I'm trying to build a quiz that uses react-modal to provides hints. 我正在尝试建立一个使用react-modal提供提示的测验。 I will need multiple modals inside the quiz. 我将在测验中需要多个模态。 I'm new to React so it's quite possible that I'm making a simple mistake. 我是React的新手,所以我很可能犯了一个简单的错误。

I'm not sure it matters, but I've built this using create-react-app . 我不确定是否很重要,但是我已经使用create-react-app构建了它。

My App.js looks like this: 我的App.js看起来像这样:

import React, { Component } from 'react';
import HintModal from './hintModal';
import Modal from 'react-modal';
import './App.css';

Modal.setAppElement('#root');

class App extends Component {

  state = {
    modalIsOpen: false,
    hint: ''
  };

  openModal = (hint) => {
    this.setState({ modalIsOpen: true,  hint: hint });
  }

  closeModal = () => {
    this.setState({ modalIsOpen: false, hint: '' });
  }

  render() {
    return (
      <React.Fragment>
      <h1>Modal Test</h1>
      <h2>First Modal</h2>
      <HintModal 
        modalIsOpen={this.state.modalIsOpen}
        openModal={this.openModal}
        closeModal={this.closeModal}
        hint="mango"
      />
      <hr />
      <h2>Second Modal</h2>
      <HintModal 
        modalIsOpen={this.state.modalIsOpen}
        openModal={this.openModal}
        closeModal={this.closeModal}
        hint="banana"
      />
      </React.Fragment>
    );
  }
}

export default App;

hintModal.jsx looks like this: hintModal.jsx看起来像这样:

import React, { Component } from 'react';
import Modal from 'react-modal';

const HintModal = (props) => {

    const {openModal, modalIsOpen, closeModal, hint} = props;

    return (
        <React.Fragment>
            <button onClick={ () => openModal(hint) }>Open Modal</button>
            <Modal
                isOpen={modalIsOpen}
                onRequestClose={closeModal}
                contentLabel="Example Modal"
            >
                <h2>Hint</h2>
                <p>{hint}</p>
                <button onClick={closeModal}>Close</button>
            </Modal>
            <p>We should see: {hint}</p>
         </React.Fragment>
    );

};

export default HintModal;

Here's the problem: I need the content of the modal to change based on the hint prop passed to HintModal. 这是问题所在:我需要根据传递给HintModal的hint道具来更改模式的内容。 When I output hint from outside <Modal> , it behaves as expected, displaying the value of the prop. 当我从<Modal>外部输出hint ,它的行为符合预期,显示了prop的值。 But when I output hint within <Modal> , it returns "banana" (the value of the hint prop for the second instance of HintModal) when either modal is activated. 但是,当我在<Modal>输出hint时,当任一模式被激活时,它将返回“ banana”(HintModal的第二个实例的hint道具的值)。

Any help would be greatly appreciated! 任何帮助将不胜感激!

You are controlling all of your modals with the same piece of state and the same functions to open and close the modal. 您将以相同的状态和相同的功能来控制所有模态,以打开和关闭模态。

You need to either have just one modal and then dynamically render the message inside it or you need to store a modalIsOpen variable in your state for every single modal. 您只需要有一个模态,然后在其中动态呈现消息,或者您需要为每个模态在状态中存储一个modalIsOpen变量。

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

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