I have created one component. in which I have a button in which once you click popup will be displayed. data for that popup is put in another component. below is the popup.
function Places() {
const [showModel, setShowModel] = useState(null)
const handleToggleModel = (value) => {
setShowModel(value)
}
return (
<div className='hotel-container'>
<div
className='hotel-container_groupA'
onClick={() => handleToggleModel(auli)}
>
<Article imgUrl={auli} date='Sep 26, 2021' text='Aoli' />
{showModel === auli && (
<Model onClose={() => handleToggleModel(null)}>
<div>
<img src={auli} alt='#' />
</div>
</Model>
)}
</div>
)
now on the close button component
const Model = ({ children, onClose }) => {
return (
<div className='model'>
<div className='model-content'>
{children}
<button onClick={onClose}>Close</button>
</div>
</div>
)
}
2
Answers
Issue here is your onClick for setting the showModel state to auli coinciding with close button on your popup. You should move the Model component out of div.
To fix the issue with the modal not closing in your React component, the primary aspect to check is the interaction between your handleToggleModel function and the onClose prop in the Model component. The goal is to ensure that when the close button is clicked, the showModel state is set to null, which should hide the modal.
Here’s a revised version of your Places and Model components with some suggestions:
Places Component
Model Component
Key Points to Consider
By ensuring that the handleToggleModel function properly sets the state, you should be able to show and hide the modal as expected. If there are still issues, you may want to inspect the value of showModel and the logic that determines when it gets updated.