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

问题描述 投票:0回答:1

我有线问题:

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);
javascript reactjs react-native redux react-navigation
1个回答
1
投票

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

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

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

如果需要与浏览器进行交互,请在componentDidMount()或其他生命周期方法中执行您的工作。保持render()纯粹使组件更容易思考。

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>;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.