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

208
Views
How to lift up the state from the child component?

I am looking for some guidance in this react code I have a ActionsDropDownWidget with a bunch of dropdown items being passed as children to it.

<DropdownMenu
                testId="actions-menu"
                trigger={({ triggerRef, ...props }) => (
                    <Button
                        appearance="subtle"
                        {...props}
                        iconBefore={<VerticalOverflowIcon label="more" />}
                        ref={triggerRef}
                    />
                )}
            >
                {children}
            </DropdownMenu>

Here is how I pass the children to ActionsDropDownWidget

<ActionsDropDownWidget attachment={attachment} onDeleteAttachment={onDeleteAttachment}>
                <CustomerDropdownActions
                    attachment={attachment}
                    onAttachmentDownload={onAttachmentDownload}
                    onCopyAttachmentName={onCopyAttachmentName}
                    onDeleteAttachment={onDeleteAttachment}
                />
            
        </ActionsDropDownWidget>

Here what is being rendered from CustomerDropdownActions

<DropdownItemGroup hasSeparator testId="negative-actions">
                <DropdownItem
                    onClick={(_) => setOpenDialog(true)}
                    elemBefore={
                        <TrashIcon label={t("confirmation_modal.negative_action.text")} testId={"trash-button"} />
                    }
                    description={t("action_dropdown.delete_attachment.description")}
                >
                    {t("action_dropdown.delete_attachment.text")}
                </DropdownItem>
            </DropdownItemGroup>
            {showConfirmationModal}

Now when the dropdown is rendered and so are the children in it.

When I click on the child above, its onClick tries to update the state. However, that state isn’t reflected and so the Dialog does not open.

Where am I going wrong in this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I guess the prop drilling took the best out of me.

  1. The first mistake was in ActionsDropDownWidget that it wasn't a react functional component so I couldn't manage the state in there.

    Fix?

    So what I did was to wrap the ActionsDropDownWidget in a wrapper functional component.

  2. The second mistake was I missed passing the event callback back to the parent to open the dialog box.

     <DropdownItem
        onClick={(_) => setOpenDialog(true)} << I should've called the parent callback here.
    

    Fix?

    In the child components CustomerActions and AgentActions I tell the parent via a callback function to show the modal. Since the parent ActionsDropdDownWidget is now a functional component, I managed my state there. So when child components send a event via the callback, I update a variable called openDialog and it renders the modal.

Sketch

Here is a sketch

about 4 years ago · Juan Pablo Isaza 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!