Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

211
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda