警告:无法从其他组件的功能主体内部更新组件

时间:2020-10-03 17:27:17

标签: reactjs react-native react-redux react-navigation

我从我的本机应用程序的componentDidMount内部更新组件级别状态。状态值将根据对Firebase的调用进行更新。根据状态,我可以调用一个动作,或者导航到其他屏幕,或者从同一组件渲染某些内容(Spinner)。该操作将更新一些应用程序级别的状态,从而呈现主屏幕。当我的组件级别状态loginIn为true或false时,会收到此警告。 警告的日志的前几行: 错误警告:无法从其他组件的功能主体内部更新组件。 在Switch中(由ConnectFunction创建) 在ConnectFunction中(在SceneView.tsx:122) 在StaticContainer中 在StaticContainer中(在SceneView.tsx:115) 在sureSingleNavigator中(在SceneView.tsx:114处)

import React, {Component} from 'react';
import {View} from 'react-native';
import firebase from 'firebase';
import {connect} from 'react-redux';
import {Spinner} from '../components/common';
import {persistantSignIn} from '../actions';

class Switch extends Component {
  state = {loggedIn: null};

  componentDidMount() {
    firebase.auth().onAuthStateChanged((userData) => {
      if (userData) {
        this.setState({loggedIn: true});
      } else {
        this.setState({loggedIn: false});
      }
    });
  }

  renderComponent() {
    if (this.state.loggedIn === null) {
      return (
        <View style={{flex:1}}>
          <Spinner />
        </View>
      );
    } else if (this.state.loggedIn === false) {
      this.props.navigation.push('SignInForm');
    } else {
      this.props.persistantSignIn();
    }
  }
  render() {
    return <View style={{flex:1}}>{this.renderComponent()}</View>;
  }
}

export default connect(null, {persistantSignIn})(Switch);

0 个答案:

没有答案