react-native如何在facebook登录和注销后刷新页面

时间:2015-07-17 12:41:53

标签: javascript ios facebook react-native

使用React-native构建我的第一个移动应用。 我正在使用Parse作为后端。 4个文件,3个屏幕: index.io.jsfb_login.jsuser_page.jstheme_list.js

render: function() {
    var render_screen;
    if (Global.is_signed_in()){

        return <NavigatorIOS
            style={styles.container}
            ref='nav'
            initialRoute={{
                title: 'Tour Champ',
                component: ThemeList,
                rightButtonTitle: tc.user.displayName.split(' ')[0],
                onRightButtonPress: this._handleUserDataPress
            }}/>;

    }else{
        render_screen = 'authenticate';
    }

    return (
        <Navigator
            initialRoute={{ id: render_screen}}
            renderScene={this.renderScene}
            configureScene={(route) => {
                if (route.sceneConfig) {
                    return route.sceneConfig;
                }

                return Navigator.SceneConfigs.FloatFromRight;
            }}
            onBack={() => {
                if (route.index > 0) {
                    navigator.pop();
                }
            }}
        />

    );
}

    renderScene(route, nav) {
    console.log('in renderScene');
    switch (route.id) {
        case 'authenticate':
            return <LoginScreen navigator={nav} />;
        case 'theme_list':
            return <ThemeList navigator={nav} />;
        default:
            return  <View>Error</View>;
    }
},

我可以登录和注销,事情是我必须手动刷新才能获得主页/登录页面(登录/注销后)。 问题是: 从fb_login.js方法登录后我该怎么做:

login_user(fb_user, password){
var that = this;
Parse.User.logIn(fb_user.id, password).then(function(user) {
  console.log('here in logged_in');
  tc.user = user.attributes;
  Global.initializeUser(user);
  //that.props.navigator.pop();
  //that.props.navigator.replace({id: 'theme_list'});
  that.setState({result: 'logged_in', user: user.attributes});
}, function(msg) {
  that.setState({result: 'failure logged_id'});
});

},

1 个答案:

答案 0 :(得分:0)

好的,所以我最后使用这篇文章中的事件发射器来做这件事 https://colinramsay.co.uk/2015/07/04/react-native-eventemitters.html

代码如下所示: index.io.js

var EventEmitter = require('EventEmitter');
var Subscribable = require('Subscribable');

window.EventEmitter = EventEmitter;
window.Subscribable = Subscribable;

    var TourChampIOs = React.createClass({
    mixins: [Subscribable.Mixin],
   componentWillMount() {
        this.eventEmitter = new EventEmitter();
    },

    componentDidMount() {

        this.addListenerOn(this.eventEmitter, 'logout_success',  this.logoutCallback);
    },
   logoutCallback(args){
        p('logout callback');
        this.setState({
            fake: args.someArg
        });
    },

fb_login.js

 login_user(fb_user, password){
var that = this;
Parse.User.logIn(fb_user.id, password).then(function(user) {
  tc.user = user.attributes;
  Global.initializeUser(user);
  that.props.navigator.pop();
  that.setState({result: 'logged_in', user: user.attributes});
  that.props.props.events.emit('fb_login_success', { someArg: 'argValue' });
}, function(msg) {
  that.setState({result: 'failure logged_id'});
});

},