Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda