【发布时间】:2017-07-03 22:02:43
【问题描述】:
我使用 react-navigation 来管理路线。这是我的 Home 组件:
class HomeScreen extends React.Component {
constructor(props) {
this.state = {
userProfile: {
firstname: 'John',
avatar: 'john-profile.png',
location: 'Canada',
}
}
}
componentDidMount() {
AsyncStorage.getItem('userProfile', (errs, result) => {
this.setState({userProfile: JSON.parse(result)});
});
}
render() {
return (
<View>
<Image src="{this.state.userProfile.avatar}" />
<Text>Firstname: {this.state.userProfile.firstname}</Text>
<Text>Location: {this.state.userProfile.location}</Text>
</View>
);
}
}
这是个人资料屏幕:
class ProfileScreen extends React.Component {
constructor(props) {
this.state = {
userProfile: null,
}
}
componentDidMount() {
AsyncStorage.getItem('userProfile', (errs, result) => {
this.setState({userProfile: JSON.parse(result)});
});
}
save() {
var userSavedProfile = this.state.userProfile;
userSavedProfile.firstname = "Peter";
userSavedProfile.avatar = "peter-avatar.png";
userSavedProfile.location = "EEUU";
this.setState({userProfile: userSavedProfile});
AsyncStorage.setItem('userProfile', JSON.stringify(this.state.userProfile), () => {});
}
render() {
return (
<View>
<Button title="Save" onPress={() => this.save()} />
</View>
);
}
}
当我保存新的用户信息并在标题中按下返回按钮(反应导航)时,用户配置文件是旧的,名字 = John 等...当用户按下返回按钮并刷新数据时,如何从主页更新状态?
【问题讨论】:
-
我也遇到了同样的问题,但我解决了。你能告诉我,你是如何进入个人资料屏幕的? home 是个人资料屏幕的父类吗?
-
请告诉我,然后我会在这里发布我的代码。
标签: react-native react-navigation