简体   繁体   English

反应-传递道具,导航器中的设置错误?

[英]React - passing props, wrong setup in navigator?

I am trying different ways to pass props between files. 我正在尝试以不同方式在文件之间传递道具。 I am very new to react and very bad at it so I don't understand what I'm doing wrong or the different ways of sending it. 我是个新手,对此反应很差,所以我不明白我在做什么错或发送它的不同方式。 Either way, it works between 'main' and 'favplaces' but not between login and main, which makes me think it has something to do with how App is set up. 无论哪种方式,它都可以在“主”和“收藏夹”之间工作,而不能在登录和主之间工作,这使我认为它与App的设置有关。 If I log the params that I got from Login in main then I'm getting either undefined or the app is crashing saying "undefined is not an object". 如果我在main中记录了从Login中获取的参数,那么我将得到未定义或应用崩溃的提示:“未定义不是对象”。

App: 应用程式:

const mainOptions = SwitchNavigator({
  main : Main,
  favPlaces : favPlaces,

})
const introStack = StackNavigator({
  logIn : LogIn,
  register : Register
})

const SwNavigator = SwitchNavigator({
  login : introStack,
  main : mainOptions

})

Login: 登录:

   constructor(props){
        super(props);
        this.state={
            username: "",
            password:"",

        }
        this.signIn = this.signIn.bind(this);
        this.navigate = this.props.navigation.navigate;
        this.params = this.props.navigation.state.params;
      }
     {......}
  signIn(){
        console.log("username", this.state.username)

        this.navigate({
    routeName: 'main',
    key: 'main',
    params: {
       username: this.state.username
    }
 });
  }

Main: 主要:

    constructor(props) {
            super(props);
        this.navigate = this.props.navigation.navigate;
        this.params = this.props.navigation.state.params;
    }
 handleClickFavourite = () => {
   console.log(this.state.latitude)
   this.navigate({
   routeName: 'favPlaces',
   key: 'favPlaces',
   params: {
      latitude: this.state.latitude
   }
});   

} }

favPlace: favPlace:

    constructor(props){
        super(props);
        this.navigate = this.props.navigation.navigate;
        this.params = this.props.navigation.state.params;
      }

   render()
      console.log("1", this.props.navigation.state.params.latitude);
      console.log("2", this.params.latitude);

both of these logs work. 这两个日志均有效。

I managed to get it to work. 我设法使它起作用。 I removed the mainOptions navigator and changed the navigators to this: 我删除了mainOptions导航器,并将导航器更改为:

const introStack = StackNavigator({
  loggingIn : LoggingIn,
  register : Register
})

const SwNavigator = SwitchNavigator({
  //login : introStack,
  loggingIn : introStack,
  main : Main,
  favPlaces : favPlaces,
  register : Register

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM