I have component foo.js
with the stack of cards with the 2 button (open and close modal) but in the same time i want to pass the event to the parent bar.js
.
My motivation is to change the parent class when the event occured, in this case i want the outer wrapper to be overflow hidden
when the open button get clicked and get back to normal for the close button.
// foo.js
import React from 'react'
class GridWithModal extends React.Component {
constructor(props) {
super(props);
this.state = {
lecturerList: this.props.lecturers,
lectorIndex: null,
isModalOpen: false
};
this.showModal = this.showModal.bind(this);
}
showModal(id) {
this.setState({
lectorIndex: id,
isModalOpen: true
});
}
render() {
const { lecturerList, lectorIndex, Content } = this.state;
return (
<>
{lecturerList.map((lector, index) => {
return (
// my card stuff here...
<button
onClick={e => {
this.showModal(index); // this open the modal
}}>
Open The Modal
</button>
)
})}
<div className="modal">
// modal stuff here
<button
onClick={e => this.setState({ dosenIndex: null })}> // this close the modal
Close Modal
</button>
</div>
</>
)
}
}
export default GridWithModal
// bar.js
import React from 'react'
const Layout = ({children}) => {
return (
<div className="bar"> // this is the element that i want to pass the event to add and remove class when the event occured
<main>
{children} // foo.js somewhere inside this chilldren
</main>
</div>
)
}
export default Layout