简体   繁体   English

如何在 React Navigation v5 中导航?

[英]How to Navigate in React Navigation v5?

So I was using React Navigation v4 for a while and I wanted to swap to RN v5.所以我使用 React Navigation v4 有一段时间了,我想换成 RN v5。

Thats the code I used and it was working fine:那是我使用的代码,它工作正常:

      this.props.navigation.navigate("Tag", {
        date: Moment.utc(this.state.activityDate).format("YYYY-MM-DD"),
      }); 

I then tried to change it because the Navigator setup changed so I changed it to this:然后我尝试更改它,因为 Navigator 设置已更改,所以我将其更改为:

      this.props.navigation.navigate("CheckStack", {
        screen: "Tag",
        params: { date: Moment.utc(this.state.activityDate).format("YYYY-MM-DD")}
      }); 

However this does not work somehow I get this error:但是,这不起作用,我收到此错误:

 ERROR  The action 'NAVIGATE' with payload {"name":"CheckStack","params":{"screen":"Tag","params":{"date":"2022-11-30"}}} was not handled by any navigator.

Do you have a screen named 'CheckStack'?

I think the screen is setup properly because when I set a title in the Navigator for that screen it changes what should be changed and I can use the navigation for example the Tab navigation works.我认为屏幕设置正确,因为当我在导航器中为该屏幕设置标题时,它会更改应更改的内容,并且我可以使用导航,例如 Tab 导航有效。

Also: I have seen that they changed params.另外:我看到他们改变了参数。 Is it still possible to pass down a value to params without Navigation?是否仍然可以在没有导航的情况下将值传递给参数?

I had this navigationOptions on my class based component screen:我在基于 class 的组件屏幕上有这个导航选项:

  static navigationOptions = ({ navigation }) => {
    return {
      headerRight: () => (
        <TouchableOpacity onPress={navigation.getParam("unlockUserHandler")}>
          <Icon
            style={{ paddingRight: 20 }}
            size={25}
            color="white"
            name="lock-open"
          />
        </TouchableOpacity>
      ),
    };
  };

Then in my componentDidMount I had it binded to a function in that class:然后在我的 componentDidMount 中,我将它绑定到 class 中的 function:

this.props.navigation.setParams({
  unlockUserHandler: this.checkUnlockHandler.bind(this),
});

I moved the logic directly into the Navigator - the Button so to say but how can I now call the Function in that screen because I cant move the Function out of it.我将逻辑直接移动到导航器中 - 可以说是按钮,但我现在如何在该屏幕中调用 Function,因为我无法将 Function 移出它。 Here is the new setup inside the Navigator.这是导航器中的新设置。

  headerRight: () => (
    <TouchableOpacity onPress={console.log("unlockUserHandler")}>
      <Icon
        style={{ paddingRight: 20 }}
        size={25}
        color="white"
        name="lock-open"
      />
    </TouchableOpacity>
  ),

In the onpress I would like to call the param unblockUserHandler but I cant get any params when I am trying to do it the new way.在 onpress 中,我想调用参数 unblockUserHandler,但是当我尝试以新方式进行操作时,我无法获得任何参数。 Is there any guidance since that change fckd up my whole app and its hard to wrap your head around what you actually need to change.是否有任何指导,因为该更改会影响我的整个应用程序,并且很难将您的头脑集中在您实际需要更改的内容上。

Old Call that worked:有效的旧电话:

navigation.getParam("unlockUserHandler")

You need to change also screen name in the navigation file like that.您还需要像那样更改导航文件中的屏幕名称。

  <Stack.Screen name="CheckStack" component={Your screen} />

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

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