react native router Actions.SCENE doing nothing

2019-05-24 06:33发布

I'm adding a simple login screen to my app, but I'm not able to call Actions.home once the user is authenticated.

I have a button that when pressed, calls a function to connect to the server and get auth status, and when successful, I call Actions.home. but nothing happens. no error or warning. just nothing.

I have tried all forms of it, Actions.home, Actions.home(), {Actions.home}, saving Actions.home as state in the constructor and then using state, ... nothing worked.

but in my other screens when I call Actions.somewhere in onPress props it works.

I read most of the issues here, and questions on StackOverflow(there weren't a lot), but couldn't understand what's wrong. I used all the solutions suggested everywhere, but nothing.

when I console.log(Actions.home), it's what I see: enter image description here

Here are my scripts:

router.js

<Scene key="root">
        <Scene key="login" title="login"  component={Login} hideTabBar hideNavBar initial />
        <Scene key="tabsRoot" tabs tabBarStyle={[styles.tabBar, styles.tabBarShadow]} >
            <Scene key="tab_home" iconName="home" icon={TabBarItem} >
                <Scene key="home" title="saba" component={Home} navBar={NavBar} />
                <Scene key="campaignHome" title="campaign" hideTabBar component={Campaign} navBar={NavBar} direction="vertical" />
            </Scene>
            ......
        </Scene>
</Scene>

login screen:

export
default class Login extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: '',
      phone: '',
      shouldLogin: false,
      shouldRegister: false,
      error: false,
    };
    this._checkAuth = this._checkAuth.bind(this);
    this._onNumberChanged = this._onNumberChanged.bind(this);
    this._isRegistered = this._isRegistered.bind(this);
    this._isNotRegistered = this._isNotRegistered.bind(this);
  }

  async _isRegistered(response) {
    var user = response.payload.user;
    this.state = {
      name: user.name,
      phone: user.phone,
      shouldLogin: true,
    };

    Actions.home;
  }

  _isNotRegistered(response) {
    var error = response.error;
    if (!error.code === NOT_REGISTERED_CODE) {
      this.setState({
        shouldLogin: false,
        shouldRegister: false,
        error: true,
      })
      throw new AuthException("server responded with unrecognised object");
    }

    this.setState({
      shouldLogin: false,
      shouldRegister: true,
      error: false,
    })
  }

  _checkAuth() {

    var {
      phone
    } = this.state;

    var data = {
      phone: phone,
    }

    let url = buildQuery(AuthTypes.LOGIN, data);

    console.log("usl: " + url);

    //connect to server
    ....
    if(response.success) {
      this._isRegistered(response);
    } else {
      this._isNotRegistered(response);
    }
  }

  _onNumberChanged(event) {
    var phone = event.nativeEvent.text;
    this.setState({
      phone: phone,
    });
  }

  render() {
      return ( < View style = {
            [styles.container]
          } >
          < View style = {
            [styles.imgContainer]
          } >
          < Image source = {
            require('./../Images/login.png')
          }
          width = "200"
          height = "200" / >
          < /View>
                <View style={[styles.form]}>
                    <View style={[styles.inputContainer]}>
                        <TextInput style={[styles.input]} placeholder="enter your phone number" onChange={ this._onNumberChanged } maxLength={12} / >
          < /View>
                    <TouchableWithoutFeedback onPress={ this._checkAuth } >
                        <View style={[styles.submitContainer]}>
                            <View>
                                <Text style={[styles.text, styles.loginButton]}>login</Text >
          < /View>
                        </View >
          < /TouchableWithoutFeedback>
                </View >
          < /View>
        )
    }
}

2条回答
冷血范
2楼-- · 2019-05-24 06:42

you should call Actions.tabsRoot and make home scene initial.

<Scene key="home" title="saba" component={Home} navBar={NavBar} initial/>
查看更多
倾城 Initia
3楼-- · 2019-05-24 07:06

I think the problem is that you are trying to go from login to a Scene which is nested inside of another scene. From the context of login, you would have Actions.tabsRoot available to you since it is an adjacent scene.

Here is a quick example I built using react-native-router-flux to test this.

<Router>
   <Scene key='pageOne' component={PageOne} initial={true}/>
   <Scene key='home'>
     <Scene key='pageTwo' component={PageTwo}/>
     <Scene key='pageThree' component={PageThree}/>
   </Scene>
</Router>

From pageOne I can call Actions.home and it transitions to the first Scene in that stack or to the Scene in that stack that has initial turned on. However, If I try calling Actions.pageTwo from pageOne it does not work.

Hope this helps. :)

查看更多
登录 后发表回答