Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
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;
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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(!setCheckboxCondition);
        };
        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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!