警告:无法在现有状态转换期间更新(例如在“render”中)

3

我在这行代码遇到了问题:

if (information && information.data && information.data.login == 1) navigation.navigate('DrawerNavigator')

我收到了一个警告:

警告:不能在现有状态转换(例如在render中)期间进行更新。渲染方法应是props和state的纯函数。而我的视图没有被渲染。

但是,如果information.data.login == 0 并且调用此行代码 if (information && information.data && information.data.login == 0) navigation.navigate('StackNavigator') 一切正常,我的视图会被渲染。

完整代码:

import React, { Component } from 'react';
import { View, ActivityIndicator } from 'react-native';
import { connect } from "react-redux";
import { postDeviceid } from '../actions/deviceid';
import { ErrorScreen } from './ErrorScreen';
import { styles } from './AppScreen.style';
import PropTypes from 'prop-types';

class AppScreen extends Component {
  
  componentDidMount() {
    this.props.dispatch(postDeviceid());
  };

  render() {
    const { information, error, loading, navigation } = this.props;
    const isLoged = () => {
      if (loading) {
        return <ActivityIndicator size="large" color="#0000ff" />
      }
      if (error && error.status > 0) {
        return <ErrorScreen error={error}></ErrorScreen>
      } else {
      if (information && information.data && information.data.login == 0) navigation.navigate('StackNavigator')
      if (information && information.data && information.data.login == 1) navigation.navigate('DrawerNavigator')
      }
    };

    return (
      <View style={styles.container}>
        {isLoged()}
      </View>
    );
  }
};

const mapStateToProps = ({ deviceid }) => ({
  information: deviceid.information,
  error: deviceid.error,
  loading: deviceid.loading
});

AppScreen.propTypes = {
  loading: PropTypes.bool.isRequired,
  error: PropTypes.array.isRequired,
  information: PropTypes.object.isRequired
};

export default connect(mapStateToProps)(AppScreen);


你不应该在render函数中使用setState或类似的东西,可以参考这个答案:https://stackoverflow.com/a/39542218/6114081 - Eliâ Melfior
1个回答

4
这是因为你在render函数内部调用了状态转换(navigation.navigate)。你希望在组件已挂载并且渲染时调用它。您可以使用props进行有条件的呈现。例如,如果传入true的加载状态,请在render方法中测试它并返回您的加载组件。
请将逻辑保持在render方法之外,因为它应该是纯的。
render()函数应该是纯的,这意味着它不会修改组件状态,每次调用时都返回相同的结果,并且不直接与浏览器交互。如果需要与浏览器交互,请在componentDidMount()或其他生命周期方法中执行工作。使render()纯化使组件更容易思考。
import React, { Component } from "react";
import { View, ActivityIndicator } from "react-native";
import { connect } from "react-redux";
import { postDeviceid } from "../actions/deviceid";
import { ErrorScreen } from "./ErrorScreen";
import { styles } from "./AppScreen.style";
import PropTypes from "prop-types";

class AppScreen extends Component {
  state = {};

  componentDidMount() {
    this.props.dispatch(postDeviceid());
    this.isLoged();
  }

  isLoged = () => {
    const { information, navigation } = this.props;

    if (information && information.data && information.data.login == 0)
      navigation.navigate("StackNavigator");
    if (information && information.data && information.data.login == 1)
      navigation.navigate("DrawerNavigator");
  };

  render() {
    const { information, error, loading, navigation } = this.props;

    if (loading) {
      return <ActivityIndicator size="large" color="#0000ff" />;
    }
    if (error && error.status > 0) {
      return <ErrorScreen error={error} />;
    }

    return <View style={styles.container}>
    Youre page content
    </View>;
  }
}

你能解释一下为什么在StackNavigator的情况下它可以工作吗?我的意思是,在DrawerNavigator的情况下视图没有被渲染? - 15kprogrammer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接