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

360
Visualizações
React Typescript: SetState inside React.FC to change value in React.Component

I have an React.Component with an state modalVisible to open an Modal:

<Modal
   visible={this.state.modalVisible}
>
   <FormStructure
      record={this.state.selectedRecord}
      question={this.state.question}
      dropdownItems={this.state.dropdownItems}
   />
</Modal>

After the Modal opens the React.FC <FormStrucutre ... /> gets rendered and the Problem is that I dont know how to change the state value modalVisible inside the React.FC:

 const Submit = () => {
        fetch('api/Call/Save', {
            headers: { 'Content-Type': 'application/json' },
            method: 'POST',
            body: JSON.stringify({
                'No': form.getFieldValue('no')
            })
        })
            .then(() => this.setState({modalVisible: false}); //TS2532  (TS) Object is possibly 'undefined'.
    };
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have to pass the closeModal method to React.FC <FormStructure />

// class component

<Modal
   visible={this.state.modalVisible}
>
   <FormStructure
      record={this.state.selectedRecord}
      question={this.state.question}
      dropdownItems={this.state.dropdownItems}
      closeModal={() => this.setState({modalVisible: false})}
   />
</Modal>

Use the props in FormStructure

// FormStructure.tsx

const FormStructure = (props: any) => {
  const {record, question, dropdownItems, closeModal} = props;

  const onSubmit = () => {
     ....
     closeModal();
  }

}
about 4 years ago · Santiago Trujillo Relatório

0

A common pattern is to pass callbacks in as props, and child components call callbacks when events happen. In this case you could expose an onSave prop:

// parent
<Modal
   visible={this.state.modalVisible}
>
   <FormStructure
      record={this.state.selectedRecord}
      question={this.state.question}
      dropdownItems={this.state.dropdownItems}
      onSave={() => this.setState({modalVisible: false})}
   />
</Modal>


// child
class FormStructure extends React.Component {
   const Submit = () => {
        fetch('api/Call/Save', {
           // ...
        })
            .then(() => this.props.onSave();
    };
}

about 4 years ago · Santiago Trujillo 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