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

256
Visualizações
Calling function from parent to child component in React

Trying to get my head around this idea - I believe it may be going against design rules for React - but still can't figure out how to do it better.

I've got an application with quite a few modal forms that may pop up - and I'm trying to ensure they're all consistent in the UI aspect. As a result, I made a "GenericModal" component, which can take title / formComponent or body/buttons. This works well and got it to populate the component.

The problem is that I can't figure out how I get my buttons to interact with the formComponent. I've tried passing refs through, but for some reason, I couldn't get that to work at 2 am, gave up, and went to bed.

Can anyone suggest anything that I'm missing? I have considered that I'm trying to get the form component to do too much (i.e, I should be passing a formik instance to the form, so that I can then interact with it from the component hosting the ).

function GenericModal(props) {
  return (
    props.show && (
      <Modal
        show={props.show}
        onHide={props.handleClose}
        backdrop="static"
        keyboard={false}
      >
        <Modal.Header closeButton={props.closeButton && props.closeButton}>
          <Modal.Title>{props.title ? props.title : "Modal"}</Modal.Title>
        </Modal.Header>

        <Modal.Body>
          {props.formComponent ? props.formComponent : props.body && props.body}
        </Modal.Body>

        {props.buttons && (
          <Modal.Footer>
            {props.buttons &&
              props.buttons.map((button) => (
                <Button
                  variant={button.style}
                  key={button.key}
                  onClick={() => handleButtonClick(button.key)}
                >
                  {button.text}
                </Button>
              ))}
          </Modal.Footer>
        )}
      </Modal>
    )
  );
}

export default GenericModal;

The buttons will call handleButtonClick, which for "submit" or "reset" I was originally thinking to fire functions on formComponent.

This component is then used as follows:

      {showNewModal && (
        <GenericModal
          show={showNewModal}
          handleClose={closeNewModal}
          title="New Asset"
          formComponent={<AssetForm asset={new Asset()} />}
          closeButton={true}
          buttons={
            { text: "Create", style: "primary", key: "submit" }
          }
        />
      )}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Not sure I fully understood the problem, but it sounds like you want to make sure you have control over what happens when the button is clicked in the GenericModal?

Can you just pass the onClick handler as a prop instead of the key?

So inside your GenericModal you'd then use the button like this:

<Button onClick={props.button.onClick} />

This way your modal button can call a callback in the parent component and you can handle it in any way you'd like.

about 4 years ago · Juan Pablo Isaza Relatório

0

I solved this by passing an object to the asset form from GenericModal, and having it populate the submit/reset functions into that object, so I could call that from the parent component on button press (so that Formik can validate the data, then pass back to the modal).

Bit of a hacky way of doing it, probably the wrong way, but it works!

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