Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Modal show/hide logic

I have a modal that pops up on a dashboard if a condition is true and renders a checkbox. I can't seem to toggle to Modal off on the onClick function. Here is an example of the code.

Dashboard

const conditionalAgreement = false;
<Modal showModal={showModal} conditionalAgreement={conditionalAgreement} />

Modal

const Modal = ({ conditionalAgreement }) => {

    const [showModal, setShowModal] = useState(false);    
    const [checkboxCondition, setCheckboxCondition = useState(false);

    useEffect(() => {
        if (conditionalAgreement) {
            setShowModal(true);
        }
    }, [conditionalAgreement]);

    const OnChangeHandler = () => {
        setCheckboxCondition(!setCheckboxCondition);
    };

    const OnClickHandler = () => {
        setShowModal(false);
    };

    return (
            <div className={css.modal}>
                <div className={css.checkbox}>
                     <CheckboxComponent
                        value={checkboxCondition}
                        onChange={OnChangeHandler}
                        description={tick this box"}
                    />
                </div>
                <div className={css.buttonContainer}>
                    <ButtonComponent
                        onClick={OnClickHandler}
                      >
                        Save
                    </ButtonComponent>
                </div>
            </div>
    );
};

export default Modal;
like image 521
ERLay Avatar asked Oct 14 '25 18:10

ERLay


1 Answers

Dashboard:

     const Dashboard = () => {
       const [showModal, setShowModal] = useState(false);

       return (
         {showModal && (
          <Modal showModal={showModal} closeModal={() => setShowModal(false)} />
         )}
       )
     }

Modal:

   
    const Modal = ({ showModal, closeModal }) => {
        const [checkboxCondition, setCheckboxCondition] = useState(false);

        const onChangeHandler = () => {
            setCheckboxCondition(!checkboxCondition);
        };
        const onClickHandler = () => {
            closeModal();
        };

        return (
                <div className={css.modal}>
                    <div className={css.checkbox}>
                         <CheckboxComponent
                            value={checkboxCondition}
                            onChange={onChangeHandler}
                            description={tick this box"}
                        />
                    </div>
                    <div className={css.buttonContainer}>
                        <ButtonComponent
                            onClick={onClickHandler}
                          >
                            Save
                        </ButtonComponent>
                    </div>
                </div>
        );
    };

Now, as mention by @RobinZigmond something in your Dashboard component should set showModal to true so that your Modal appears.

like image 196
Baptiste F. Avatar answered Oct 21 '25 03:10

Baptiste F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!