我在这行代码遇到了问题:
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);