反应模态不显示

时间:2018-05-02 15:34:28

标签: reactjs

我已在下面附上我的主要代码。基本上当页面显示并且我点击删除按钮时,不会弹出任何模态。我没有看到任何编译错误,但开发控制台显示以下内容:

Warning:React.createElement: type is invalid--expected a string (for built in components) or a class/function (for composite components) but got undefined. You likely forgot to export your component from the file its defined in. Check your code at ModalItem.js:27, ModalItem.js 26.

这是我的代码: ModalItem.js

import React from 'react';
import { Link } from 'react-router-dom';
 import PropTypes from 'prop-types';
import Item Service from './ItemService';
    class Modal extends React.Component {
      constructor(props) {
        super(props);
        this.addItemService = new ItemService();
        this.handleSubmit = this.handleSubmit.bind(this);
      }

      handleSubmit(event) {  //this is showing error at the curly bracket. unexpected token. not sure whats wrong here
        event.preventDefault();
        this.addItemService.deleteData(this.props.obj._id);
      }
      render() {
        // Render nothing if the "show" prop is false
        if (!this.props.show) {
          return null;
        }
        else {
          return (
            <div className="static-modal">
              <Modal.Dialog>
                <Modal.Header>
                  <Modal.Title>Ready to Delete Student</Modal.Title>
                </Modal.Header>

                <Modal.Body>Are you sure you want to delete this Student?</Modal.Body>

                <Modal.Footer>
                  <Button onClick={this.props.onClose}>Close</Button>
                  <form onSubmit={this.handleSubmit}>
                    <input type="submit" value="Delete" className="btn btn-danger" />
                  </form>

                </Modal.Footer>
              </Modal.Dialog>
            </div>
          );
        }
      }
    }
    }

    export default Modal;

TableRow.js

class TableRow extends Component {

  constructor(props) {
      super(props);
      this.addItemService = new ItemService();
      this.state = {isOpen: false}; 
  }

  toggleModal = () => {
    this.setState({
      isOpen: !this.state.isOpen
    });
  }


  render() {
    return (
        <tr>
          <td>
            {this.props.obj._id}
          </td>
          <td>
            {this.props.obj.item}
          </td>
          <td>
          <Link to={"/edit/"+this.props.obj._id} className="btn btn-primary">Edit</Link>
        </td>
          <td>
          <button onClick={this.toggleModal}>
            Delete
          </button>
            <Modal show={this.state.isOpen}
            onClose={this.toggleModal}>
            </Modal>

          </td>
        </tr>
    );
  }
}

export default TableRow;

index.js

import App from './App';
import AddItem from './components/AddItem';
import IndexItem from './components/IndexItem';
import EditItem from './components/EditItem';

ReactDOM.render(
  <Router>
      <div>
        <Route exact path='/' component={App} />
        <Route path='/add-item' component={AddItem} />
        <Route path='/index' component={IndexItem} />
        <Route path='/edit/:id' component={EditItem} />
      </div>
  </Router>,
  document.getElementById('root')
);

你能告诉我我做错了什么以及这个错误意味着什么?

1 个答案:

答案 0 :(得分:2)

正如评论中指出的那样,您可能没有导入必要的组件:

import React from 'react';
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import ItemService from './ItemService';
import { Modal, Button } from 'react-boostrap' // or reactstrap?