【发布时间】:2017-10-17 07:05:26
【问题描述】:
我是 React 新手,由于这个错误,我无法渲染我的应用程序。似乎我试图渲染为元素的数据由于在卸载时尝试设置状态而无法渲染?
我不确定我是如何得到这个错误的,因为我在componentDidMount 中设置Data 的状态。
我该如何解决这个问题?
错误:试图更新已卸载(或未能安装)的组件
class Profile extends React.PureComponent {
static propTypes = {
navigation: PropTypes.object,
handleLogout: PropTypes.func,
user: PropTypes.object,
};
static navigationOptions = {
headerVisible: true,
title: 'Profile',
};
constructor(props) {
super(props);
this.state = {
data: [],
loading: true
};
}
componentDidMount() {
fetch("http://10.0.2.2:8080/combined", { method: 'get' })
.then(response => response.json())
.then(data => {
this.setState({data: data,});
})
.catch(function(err) {
//
})
}
render() {
const { user } = this.props;
let email;
if (user) {
email = user.rows[0].ACCTNO;
}
return (
<ContentWrapper>
<View style={styles.container}>
<Image style={styles.header} source={images.profileHeader} />
<View style={styles.body}>
<Avatar email={email} style={styles.avatar} />
{
this.state.data.map(function(rows, i){
this.setState({mounted: true});
return(
<View key={i}>
<Text>{rows.FIRSTNAME}</Text>
</View>
);
})
} <Text style={styles.email}>{_.capitalize(email)}</Text>
<Button
title="Log out"
icon={{ name: 'logout-variant', type: 'material-community' }}
onPress={this.logout}
style={styles.logoutButton}
/>
</View>
</View>
</ContentWrapper>
);
}
}
export default Profile;
【问题讨论】:
-
您的地图功能是发生错误的原因。你在 render() 中调用 setState。
标签: javascript reactjs react-native