【发布时间】: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);
【问题讨论】:
-
你不应该在你的渲染函数中使用 setState 或类似的东西,参考这个答案:stackoverflow.com/a/39542218/6114081
标签: javascript reactjs react-native redux react-navigation