Parse platform react native和parse,currentuser在登录和cmd+;R
我在React Native和Parse JS SDK方面遇到问题。Parse platform react native和parse,currentuser在登录和cmd+;R,parse-platform,reactjs,react-native,Parse Platform,Reactjs,React Native,我在React Native和Parse JS SDK方面遇到问题。 我正在使用 我已经建立了一个登录,注册和主视图,注册和登录工作良好,但在我登录->指向主视图,当我刷新我的模拟器中的应用程序(CMD+R)时,它会再次将我带回到登录视图,我应该被带到主视图 如您所见,我为initialComponent设置了一个状态: this.state = { InitialComponent : ((!currentUser) ? LoginView : MainView)
我正在使用 我已经建立了一个登录,注册和主视图,注册和登录工作良好,但在我登录->指向主视图,当我刷新我的模拟器中的应用程序(CMD+R)时,它会再次将我带回到登录视图,我应该被带到主视图 如您所见,我为initialComponent设置了一个状态:
this.state = {
InitialComponent : ((!currentUser) ? LoginView : MainView)
};
这允许我的导航器检查currentUser是否为null,然后加载LoginView作为初始组件,否则设置主视图(用户登录)
“严格使用”;
var React=require('React-native');
var MainView=require('./MainView');
var LoginView=require('./LoginView');
var Parse=require('Parse')。Parse;
var ParseReact=require('parse-react');
初始化(“mykey”、“mykey”);
变量{
评估学,
样式表,
文本,
看法
文本输入,
触控高光,
领航员,
组成部分
}=反应;
类MyApp扩展组件{
建造师(道具){
超级(道具);
var currentUser=Parse.User.current();
console.log('当前用户:'+当前用户);
this.state={
InitialComponent:(!currentUser)?登录视图:主视图)
};
}
render(){
返回(
{
从右侧返回Navigator.sceneconfig.floatfrom;
}}
renderScene={(路线、导航器)=>{
if(路由组件){
返回React.createElement(route.component,{navigator});
}
}}/>
);
}
}
注册表组件('MyApp',function(){return MyApp});
在我的Xcode控制台中,每次刷新后,我都会发现当前用户为空,即使我以前登录过。在我的解析应用程序上,我可以看到新会话已经创建
在我的登录视图中
'use strict';
var React = require('react-native');
var SignUp = require('./SignUp');
var MainView = require('./MainView');
var {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
TouchableHighlight,
Navigator,
AlertIOS,
Component
} = React;
var styles = StyleSheet.create({
container : {
flex: 1,
padding: 15,
marginTop: 30,
backgroundColor: '#0179D5',
},
text: {
color: '#000000',
fontSize: 30,
margin: 100
},
headingText: {
color: '#fff',
fontSize: 40,
fontWeight: '100',
alignSelf: 'center',
marginBottom: 20,
letterSpacing: 3
},
textBox: {
color: 'white',
backgroundColor: '#4BB0FC',
borderRadius: 5,
borderColor: 'transparent',
padding:10,
height:40,
borderWidth: 1,
marginBottom: 15,
},
greenBtn: {
height: 36,
padding: 10,
borderRadius: 5,
backgroundColor: '#2EA927',
justifyContent: 'center'
},
signUpButton: {
marginTop: 10,
height: 36,
padding: 10,
borderRadius: 5,
backgroundColor: '#FF5500',
justifyContent: 'center'
},
btnText: {
color : '#fff',
fontSize: 15,
alignSelf: 'center'
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center'
},
loginForm : {
flex:1,
marginTop:100
}
});
class LoginView extends Component {
constructor(props) {
super(props);
this.state = {
username: '',
password: ''
};
}
checkLogin() {
var success = true;
var state = this.state;
for(var key in state){
if(state[key].length <= 0){
success = false;
}
}
if(success) {
this._doLogin();
} else {
//show alert
AlertIOS.alert('Error','Please complete all fields',
[{text: 'Okay', onPress: () => console.log('')}]
);
}
}
goMainView() {
this.props.navigator.push({
title: "Home",
component: MainView
});
}
goSignUp() {
this.props.navigator.push({
title: "Sign Up",
component: SignUp
});
}
_doLogin() {
var parent = this;
Parse.User.logIn(this.state.username, this.state.password, {
success: function(user) {
parent.goMainView();
},
error: function(user, error) {
AlertIOS.alert('Login Error', error.message,
[{text: 'Okay', onPress: () => console.log('')}]
);
}
});
}
onUsernameChanged(event) {
this.setState({ username : event.nativeEvent.text });
}
onPasswordChanged(event) {
this.setState({ password : event.nativeEvent.text });
}
render() {
return(
<View style={styles.container}>
<View style={styles.loginForm}>
<Text style={styles.headingText}>
MyStatus
</Text>
<TextInput style={styles.textBox}
placeholder='Username'
onChange={this.onUsernameChanged.bind(this)}
placeholderTextColor='#fff'
autoCorrect={false}
>
</TextInput>
<TextInput style={styles.textBox}
placeholder='Password'
onChange={this.onPasswordChanged.bind(this)}
placeholderTextColor='#fff'
password={true}
>
</TextInput>
<TouchableHighlight style={styles.greenBtn}
underlayColor='#33B02C'
onPress={this.checkLogin.bind(this)}>
<Text style={styles.btnText}>Login</Text>
</TouchableHighlight>
<TouchableHighlight style={styles.signUpButton}
underlayColor='#D54700'
onPress={this.goSignUp.bind(this)}>
<Text style={styles.btnText}>Sign Up</Text>
</TouchableHighlight>
</View>
</View>
);
}
}
module.exports = LoginView;
“严格使用”;
var React=require('React-native');
var SignUp=require(“./SignUp”);
var MainView=require('./MainView');
变量{
评估学,
样式表,
文本,
看法
文本输入,
触控高光,
领航员,
AlertIOS,
组成部分
}=反应;
var styles=StyleSheet.create({
容器:{
弹性:1,
填充:15,
玛金托普:30,
背景颜色:“#0179D5”,
},
正文:{
颜色:'#000000',
尺寸:30,
保证金:100
},
标题文字:{
颜色:“#fff”,
尺寸:40,
fontWeight:'100',
对齐自我:“中心”,
marginBottom:20,
字距:3
},
文本框:{
颜色:'白色',
背景颜色:“#4BB0FC”,
边界半径:5,
边框颜色:“透明”,
填充:10,
身高:40,
边框宽度:1,
marginBottom:15,
},
绿色BTN:{
身高:36,
填充:10,
边界半径:5,
背景颜色:“#2EA927”,
为内容辩护:“中心”
},
注册按钮:{
玛金托普:10,
身高:36,
填充:10,
边界半径:5,
背景颜色:“#FF5500”,
为内容辩护:“中心”
},
btnText:{
颜色:“#fff”,
尺寸:15,
自我定位:“中心”
},
按钮文字:{
尺码:18,
颜色:'白色',
自我定位:“中心”
},
登录信息:{
弹性:1,
玛金托普:100
}
});
类LoginView扩展了组件{
建造师(道具){
超级(道具);
此.state={
用户名:“”,
密码:“”
};
}
checkLogin(){
var成功=真;
var state=this.state;
for(var键处于状态){
if(state[key].length console.log(“”)}
);
}
}
goMainView(){
这个是.props.navigator.push({
标题:“家”,
组件:MainView
});
}
goSignUp(){
这个是.props.navigator.push({
标题:“注册”,
组成部分:注册
});
}
_多洛金(){
var parent=此;
Parse.User.logIn(this.state.username、this.state.password、{
成功:功能(用户){
parent.goMainView();
},
错误:函数(用户,错误){
AlertIOS.alert('Login Error',Error.message,
[{text:'ok',onPress:()=>console.log('')}]
);
}
});
}
onUsernameChanged(事件){
this.setState({username:event.nativeEvent.text});
}
onPasswordChanged(事件){
this.setState({密码:event.nativeEvent.text});
}
render(){
返回(
MyStatus
登录
注册
);
}
}
module.exports=LoginView;
我做错了吗?好心的建议。还是解析localstorage/session有什么问题?因为React Native只提供异步存储,我们被迫进行
parse.User.current()
异步调用,返回一个承诺。因为您已经在使用Parse+React
,所以处理这个问题非常容易。根据用户是否登录而更改的组件(MyApp)应订阅ParseReact.currentUser
。这将与当前用户保持同步,并允许组件在用户登录或注销时自动更新。为了演示这一点,请查看GitHub repo中的AnyBudget:首先,在我的例子中,我使用了ParseReact.Mutation,它工作得非常好。
之后,我得到了一个要求,即某些字段必须是唯一的,所以我使用Parse.Cloud
来解决这个问题
// For normally situaltion is work fine.
ParseReact.Mutation.Set(this.data.myObject, {
normal_field: this.state.value
}).dispatch();
// But if I use Parse.Cloud. Is there any solution to sync data to ParseReact.currentUser?
Parse.Cloud.run('setUniqueField', {
userID: this.data.user.id.objectId,
uniqueField: this.state.value,
}, {
success: function (response) {
// It doesn't work!
// ParseReact.currentUser.update();
this.props.navigator.pop();
}.bind(this),
error: function (error) {
console.log('[Parse Cloud Error]: ', error);
}
});
@安德鲁(独自)的解决方案对我不起作用。如果其他人被卡住了,下面是发生的情况:订阅Parse.User.current()
无效,并且this.data.User
始终为空
要解决这个问题,我必须改变:
var Parse=require('Parse')。Parse;
var ParseReact=require('
// For normally situaltion is work fine.
ParseReact.Mutation.Set(this.data.myObject, {
normal_field: this.state.value
}).dispatch();
// But if I use Parse.Cloud. Is there any solution to sync data to ParseReact.currentUser?
Parse.Cloud.run('setUniqueField', {
userID: this.data.user.id.objectId,
uniqueField: this.state.value,
}, {
success: function (response) {
// It doesn't work!
// ParseReact.currentUser.update();
this.props.navigator.pop();
}.bind(this),
error: function (error) {
console.log('[Parse Cloud Error]: ', error);
}
});
import Parse from 'parse/react-native';
Parse.User.currentAsync()
.then((currentUser)=>{
if (currentUser) {
Alert.alert('', JSON.stringify(currentUser))
}
});