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

时间:2019-02-15 10:38:17

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

我对线路有疑问

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

我收到警告:

  

警告:在现有状态转换过程中无法更新(例如   在render中)。渲染方法应该是道具的纯粹功能   和状态。而且我的视图没有呈现

但是如果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);

1 个答案:

答案 0 :(得分:1)

这是因为您要在渲染函数中调用状态转换(navigation.navigate)。您要在安装组件然后进行渲染时调用它。您可以使用道具有条件地渲染。因此,例如,如果传入了true的加载状态,请对其进行测试,然后在render方法中返回您的加载组件。

将逻辑保持在render方法之外,因为它应该是纯逻辑的。

  

render()函数应该是纯函数,这意味着它不会修改组件状态,每次调用时都返回相同的结果,并且不与浏览器直接交互。

     

如果需要与浏览器进行交互,请在   componentDidMount()或其他生命周期方法。保持   render()pure使组件更容易考虑。

     

https://reactjs.org/docs/react-component.html#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>;
  }
}