如何从父组件打开或关闭 reactstrap 模态
How to make reactstrap modal open or close from parent component
我正在尝试从父组件按钮打开/关闭 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"));
您正在传递状态变量 isModalOpen
作为指示它是否打开的变量,以及用于切换它的函数。请改用 this.toggle
进行切换。
<ModalExample
isOpen={this.state.isModalOpen}
toggle={this.toggle}
/>
您还在 ModalExample
组件中使用了 toggleModalView
属性,但是您将函数作为 toggle
传递,因此您应该改用 this.props.toggle
。您还将 isModalOpen
作为 isOpen
传递,因此您应该改用 this.props.isOpen
。
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**
我正在尝试从父组件按钮打开/关闭 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"));
您正在传递状态变量 isModalOpen
作为指示它是否打开的变量,以及用于切换它的函数。请改用 this.toggle
进行切换。
<ModalExample
isOpen={this.state.isModalOpen}
toggle={this.toggle}
/>
您还在 ModalExample
组件中使用了 toggleModalView
属性,但是您将函数作为 toggle
传递,因此您应该改用 this.props.toggle
。您还将 isModalOpen
作为 isOpen
传递,因此您应该改用 this.props.isOpen
。
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**