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

123
Visualizações
Can't perform a React state update on an unmounted component #434

Im trying to add step to realtime database firebase. When I add the step, it works in the database but then the modal closes and I dont have any error but the modal closes without any reason

I think the problem is when I call the firebase to update it (in addTodo function), but I have no ideas why.

TodoModal.js :

  componentDidMount = async () => {
    this.mounted = true;
  };

  compunentWillUnmount = () => {
    this.mounted = false;
  }

  addTodo = async () => {
    if (this.state.newStep !== "") {
       try {
        await firebase
          .database()
          .ref("Tasks")
          .child(firebase.auth().currentUser.uid).child(firebase.auth().currentUser.uid)
          .child(this.props.task.taskname)
          .child("Steps")
          .child(this.state.newStep)
          .update({ title: this.state.newStep, status: false, dateAdd: date });
        this.props.addStep({ title: this.state.newStep, status: false, dateAdd: date }); 
        if (this.mounted) {
          this.setState({ newStep: "" });//to reinitialize the newStep field
        }

      } catch (error) {
        console.log(error);
      } 
    } else {
      Alert.alert("","field is required");
    }
  };
  };




  render() {
    return (
            <View>
              <TextInput
                onChangeText={(text) => this.setState({ newStep: text })}
                value={this.state.newStep}
              />
              <TouchableOpacity
                onPress={() => this.addTodo()}
              >
              </TouchableOpacity>
            </View>
    );
  }
}

);

TodoTask.js :

render() {
    const task = this.props.task;
    return (
      <View>
        <Modal
          animationType="slide"
          visible={this.state.showTaskVisible}
          onRequestClose={() => this.toggleisModal()}
        >
          <TodoModal task={task} closeModal={() => this.toggleisModal()} />
        </Modal>
       </View>
      )
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That error happens when an async function ends and the component that triggered that function was unmounted, so the result of the async function tries to update the state of something that is not there anymore.

Usually, to prevent this, you have to either reject the promise OR check if the component is still mounted before trying to modify its state.

You could keep track of the mounted state doing something like

componentDidMount = () => {
  this.mounted = true;
}

compunentWillUnmount = () => {
  this.mounted = false;
}

then you can check if this.mounted before trying to change the state.

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