简体   繁体   中英

can't render react-modal as reusable component

I have an app with a list of meal, and clicking on a meal should make a modal appear on the screen with further information about the meal.

So I am trying to code a basic modal as a reusable component using the react-modal package.

However when I try to 'activate' the modal it does not work. the openModal method does get fired but the modal does not show up on the screen.

App.js:

import React from 'react';

import MealCard from './MealCard';
import MealModal from './MealModal';


export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      modalIsOpen: false
    }
    this.openModal = this.openModal.bind(this);

  };

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

  render() {
    return (
      <div>
        <div className="app-wrapper" style={{display: 'flex'}}>
          <div className="container">
            <div className="row">
              {[...Array(20)].map((x, i) =>
                  <div className="col-sm-6 col-xs-12 " key={i} onClick={this.openModal}>
                    <MealCard />
                  </div>
                )}
          </div>
        </div>
      </div>
      <MealModal isOpen={this.state.modalIsOpen}/>
    </div>
    );
  }
}

MealModal.js

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

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

Modal.setAppElement('#app')

export default class MealModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      modalId: 0,
      modalIsOpen: false
    };

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

  componentWillMount() {
    this.setState({modalId: 3})
  }

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

  closeModal() {
    this.setState({modalIsOpen: false});
  }
  render() {
    return (
      <Modal
        isOpen={this.props.modalIsOpen}
        onRequestClose={this.closeModal}
        style={customStyles}
        contentLabel="Meal Modal"
      >
        <div className="modal-wrapper">
          <div className="container text-center">
            <h1>Hello</h1>
            <h2>ID of this modal is {this.state.modalId}</h2>
            <h3>This is an awesome modal.</h3>
            <button onClick={this.closeModal}>close</button>
          </div>
        </div>
      </Modal>
    )
  }
}

You're passing isOpen as props and using modalIsOpen (props) in MealModal component.

As mentioned in the comment, you can just use isOpen={this.props.isOpen} . There's no sense to use two states for serving the same purpose, one is modalIsOpen in App component and other is modalIsOpen in MealModel component

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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