gpt4 book ai didi

reactjs - react 模态不显示

转载 作者:行者123 更新时间:2023-12-05 04:06:09 24 4
gpt4 key购买 nike

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

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.

这是我的代码:模态项.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;

索引.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')
);

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

最佳答案

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

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?

关于reactjs - react 模态不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com