First Check my react-bootstrap
codes:
JavaScript
x
90
90
1
export default class App extends React.Component {
2
constructor(props) {
3
super(props);
4
this.state = {
5
show: false
6
};
7
}
8
9
handleShow = (e) => {
10
this.setState({ show: true });
11
};
12
13
handleClose = (e) => {
14
this.setState({ show: false });
15
};
16
17
render() {
18
return (
19
<div className="App">
20
<button onClick={this.handleShow}>Open Modal</button>
21
<Modal
22
size="lg"
23
centered={true}
24
show={this.state.show}
25
onHide={this.handleClose}
26
aria-labelledby="md-modal"
27
>
28
<Modal.Header>
29
First Modal
30
<button
31
type="button"
32
className="close"
33
onClick={this.handleClose}
34
>
35
X
36
</button>
37
</Modal.Header>
38
<Modal.Body>
39
Please <Content clicked={this.handleClose} /> to show New Content
40
</Modal.Body>
41
</Modal>
42
</div>
43
);
44
}
45
}
46
47
class Content extends React.Component {
48
constructor(props) {
49
super(props);
50
this.state = {
51
show: false
52
};
53
}
54
55
handleShow = (e) => {
56
this.props.clicked();
57
this.setState({ show: true });
58
};
59
60
handleClose = (e) => {
61
this.setState({ show: false });
62
};
63
render() {
64
return (
65
<>
66
<button onClick={this.handleShow}>Click Here</button>
67
<Modal
68
size="lg"
69
centered={true}
70
show={this.state.show}
71
onHide={this.handleClose}
72
aria-labelledby="md-modal"
73
>
74
<Modal.Header>
75
Second Modal
76
<button
77
type="button"
78
className="close"
79
onClick={this.handleClose}
80
>
81
X
82
</button>
83
</Modal.Header>
84
<Modal.Body>New Content</Modal.Body>
85
</Modal>
86
</>
87
);
88
}
89
}
90
I need to show second modal then hide first modal after clicked on the button on first modal.
You can see demo here: https://codesandbox.io/s/xenodochial-joliot-jl3qe
the problem is the both of modal was hide when click on the button on first modal.
maybe i need to use redux in this case ?
Advertisement
Answer
so we need to track the statuses for the 2 modals… the only difference being that since modal-2 can be triggered from inside modal-1, we need to make sure that we close modal-1 when we are displaying modal-2;
I showed the statuses on the screen so that it becomes easier to track and understand;
following is the relevant code:
JavaScript
1
120
120
1
import React, { useState, useEffect } from "react";
2
import { Modal } from "react-bootstrap";
3
import "./styles.css";
4
5
const Modal1 = ({
6
modalState,
7
modalHeading,
8
toggleModalOne,
9
openTwoAndCloseOne
10
}) => {
11
const [modalOpen, setModalOpen] = useState(modalState);
12
13
useEffect(() => {
14
setModalOpen(modalState);
15
}, [modalState]);
16
17
return (
18
<Modal
19
size="lg"
20
centered={true}
21
show={modalOpen}
22
onHide={toggleModalOne}
23
aria-labelledby="md-modal"
24
>
25
<Modal.Header>
26
{modalHeading}
27
<button type="button" className="close p-2" onClick={toggleModalOne}>
28
close <i className={"mdi mdi-close"} />
29
</button>
30
</Modal.Header>
31
<Modal.Body className="p-0">
32
New Content
33
<button type="button" onClick={openTwoAndCloseOne}>
34
close first & open second
35
</button>
36
</Modal.Body>
37
</Modal>
38
);
39
};
40
41
const Modal2 = ({ modalState, modalHeading, toggleModalTwo }) => {
42
const [modalOpen, setModalOpen] = useState(modalState);
43
44
useEffect(() => {
45
setModalOpen(modalState);
46
}, [modalState]);
47
48
return (
49
<Modal
50
size="lg"
51
centered={true}
52
show={modalOpen}
53
onHide={toggleModalTwo}
54
aria-labelledby="md-modal"
55
>
56
<Modal.Header>
57
{modalHeading}
58
<button type="button" className="close p-2" onClick={toggleModalTwo}>
59
close <i className={"mdi mdi-close"} />
60
</button>
61
</Modal.Header>
62
<Modal.Body className="p-0">
63
ths is the Second modal and its New Content
64
</Modal.Body>
65
</Modal>
66
);
67
};
68
69
export default class App extends React.Component {
70
constructor(props) {
71
super(props);
72
this.state = {
73
show: false,
74
showSecond: false
75
};
76
}
77
78
toggleModalOne = (e) => {
79
this.setState({ show: !this.state.show });
80
};
81
82
toggleModalTwo = (e) => {
83
this.setState({ showSecond: !this.state.showSecond });
84
};
85
86
openTwoAndCloseOne = (e) => {
87
this.setState({ showSecond: !this.state.showSecond });
88
if (this.state.show) this.setState({ show: false });
89
};
90
91
render() {
92
return (
93
<div className="App">
94
1st modal current show status: {this.state.show.toString()}
95
<br />
96
2nd modal current show status: {this.state.showSecond.toString()}
97
<br />
98
<button type="button" onClick={this.toggleModalOne}>
99
change Modal1 status to {(!this.state.show).toString()}
100
</button>
101
<Modal1
102
modalHeading={"this is modal heading for the first mofal"}
103
modalState={this.state.show}
104
toggleModalOne={this.toggleModalOne}
105
openTwoAndCloseOne={this.openTwoAndCloseOne}
106
/>
107
{/*
108
<button type="button" onClick={this.toggleModalTwo}>
109
change Modal2 status to {(!this.state.showSecond).toString()}
110
</button> */}
111
<Modal2
112
modalHeading={"this is modal heading for the second modal..."}
113
modalState={this.state.showSecond}
114
toggleModalTwo={this.toggleModalTwo}
115
/>
116
</div>
117
);
118
}
119
}
120
complete working example forked off your code-sandbox here