【发布时间】:2016-02-28 18:27:12
【问题描述】:
我有一个 ReactNative 应用程序,它有一个返回 JSON 对象的 getUserSession 函数。我想调用这个函数,然后 setState 到来自getUserSession 的回调结果。
class AppContainer extends Component{
constructor(props){
super(props);
this.state = {
selectedTab: 'profile',
}
}
componentDidMount(){
this.fetchData();
this.setState({
loggedIn: true,
});
}
fetchData(){
userService.getUserSessionData(function(result){
// Console log function displays correct result
console.log(result);
this.setState({
user: JSON.stringify(result),
});
})
}
render() {
let currentUser = this.state.user;
return (
<TabBarIOS style={Theme.appContainer}>
<TabBarIOS.Item
title="Profile"
selected={this.state.selectedTab == "profile"}
<NavigatorIOS
initialRoute={{
title: 'Profile',
component: Profile,
passProps: {data: currentUser}
}} />
</TabBarIOS.Item>
</TabBarIOS>
);
}
};
module.exports = AppContainer;
fetchData 中的 console.log 函数呈现了预期的行为并准确显示了我想要的内容,但是在下一行它没有正确地 setState。
在 NavigatorIOS 上使用 PassProps 传递状态时,子组件 'Profile' 没有接收到 props 并返回一个 'undefined'。
我已经尝试JSON.stringify() 结果,因为我认为 setState 将允许 JSON 字符串,但这也不起作用
【问题讨论】:
标签: javascript json react-native