I’m trying to make a popup functionality but without Redux.
My popup component gets a prop called activePopup and width values true or false.
When it’s true, my popup is showing up, but I can’t change this prop to false for
closing this popup, because activeProp can be change only in the parent component.
const Popup = ({ isActive }) => {
return (
<div className={isActive ? "popup" : ""}>
<div className="popupContent">
<h2>
Welcome to popup!
<button title="close popup">X</button>
</h2>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odio neque
cupiditate officiis cum rem iste aliquam ut ipsum quidem non, aperiam
eos harum similique, architecto a minus explicabo atque error.
</p>
</div>
</div>
);
};
I trying to fix my popup component so that he/she can work like I want. Because my popup can open, but it can’t close inside the component.
3
Answers
Transfer the state that is (probably) managed in your parent component into the local state of the component via hooks.
four-eyes’s answer is good, except you can’t control the popup from the parent anymore. You can just pass the state and the state-setter as props and merge your code with
four-eyes'
code.Parent Component:
Popup component
So now you can control the state from in and out of the component
https://codesandbox.io/s/popup-wwhn4l
PopUp open and close function.