我正在尝试从父组件按钮打开/关闭reactstrap模态,但是我无法使其正常工作。
我正在将状态isModalOpen
传递给子ModalExample
组件,它的变化如我所做的调试文本字段中所示,但模态似乎没有打开。
感谢您的帮助。这是一个jsfiddle:https://jsfiddle.net/67wy5nqp/
const { Button, Modal, ModalHeader, ModalBody, ModalFooter } = Reactstrap;
class ModalExample extends React.Component {
constructor(props) {
super(props);
console.log(this.props);
}
render() {
return (
<div>
<br />
<label>
(child) this.props.isOpen
<input type="text" value={this.props.isOpen} />
</label>
<Modal
isOpen={this.props.isModalOpen}
toggle={this.props.toggleModalView}
className={this.props.className}
>
<ModalHeader toggle={this.props.toggleModalView}>
Modal title
</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.props.toggleModalView}>
Do Something
</Button>
<Button color="secondary" onClick={this.props.toggleModalView}>
Cancel
</Button>
</ModalFooter>
</Modal>
</div>
);
}
}
class SampleApp extends React.Component {
constructor(props) {
super(props);
this.state = {
isModalOpen: true
};
this.showModal = this.showModal.bind(this);
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({
isModalOpen: !this.state.isModalOpen
});
}
showModal() {
this.setState({
isModalOpen: true
});
}
render() {
return (
<div>
<ModalExample
isOpen={this.state.isModalOpen}
toggle={this.state.isModalOpen}
/>
<br />
<button className="btn btn-primary" onClick={this.toggle}>
Show Modal From Parent Component
</button>
<br />
<label>
(parent) this.state.isModalOpen:
<input type="text" value={this.state.isModalOpen} />
</label>
</div>
);
}
}
ReactDOM.render(<SampleApp />, document.querySelector("#app"));
答案 0 :(得分:1)
您正在传递状态变量isModalOpen
作为指示它是否打开的变量,以及用于切换它的函数。改为使用this.toggle
进行切换。
<ModalExample
isOpen={this.state.isModalOpen}
toggle={this.toggle}
/>
您还使用了toggleModalView
组件中的ModalExample
道具,但是您以toggle
的形式传入了函数,因此应该改用this.props.toggle
。您还将isModalOpen
作为isOpen
传递,因此应改用this.props.isOpen
。
答案 1 :(得分:0)
import React from 'react';
import { Button, Modal, ModalFooter, ModalHeader,ModalBody} from 'reactstrap';
class ModalExample extends React.Component {
constructor(props) {
super(props);
this.state = {
modal: props.initialModalState,
fade: true
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({
modal: !this.state.modal,
fade: !this.state.fade
});
}
render() {
return (
<div>
<Button color="danger" onClick={this.toggle}>TOGGLE</Button>
<Modal isOpen={this.state.modal} toggle={this.toggle}
fade={this.state.fade}
className={this.props.className}>
<ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
<ModalBody>
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={this.toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={this.toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
}
export default class SampleApp extends React.Component {
constructor(props) {
super(props);
this.state = {}
}
render() {
return (
<div>
<ModalExample initialModalState={false} />
</div>
)
}
}
**This example getting call component modal from another component**