【问题标题】:Warning: Cannot update during an existing state transition (such as within `render`)警告:在现有状态转换期间无法更新(例如在 `render` 中)
【发布时间】:2019-02-15 10:38:17
【问题描述】:

我的线路有问题:

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

我收到警告:

警告:在现有状态转换期间无法更新(例如 在render) 内。渲染方法应该是 props 的纯函数 和状态。而且我的视图没有渲染

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

【问题讨论】:

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


【解决方案1】:

这是因为您在渲染函数中调用了状态转换 (navigation.navigate)。您想在组件已安装然后渲染时调用它。您可以使用道具有条件地渲染。因此,例如,如果传入了 true 的加载状态,请对其进行测试并在 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>;
  }
}

【讨论】:

  • 你能解释一下,为什么在 StackNavigator 的情况下它可以工作?我的意思是,视图是渲染的,但如果是 od DrawerNavigator,视图不是渲染?
猜你喜欢
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2017-05-16
  • 2016-09-20
相关资源
最近更新 更多