【问题标题】:how to redirect with navigate in react native?如何在本机反应中使用导航重定向?
【发布时间】:2019-02-25 19:55:41
【问题描述】:

我有一个静态的 navigationOptions 并且我做了一个 headerRight 来放置一个按钮来断开连接。 问题是我不知道如何重定向!

这是我的导航选项:

  static navigationOptions = {
    title: "Profile",
    headerStyle: {
      backgroundColor: "#4169E1"
    },
    headerTintColor: "white",
    headerRight: (
      <Button
      onPress={() => removeItem()}
      title="Disconnect"
      color="white"
      />
    )
  };

所以它调用一个函数 removeItem() 来使用 AsyncStorage 删除项目:

export async function removeItem() {
    console.log("navigate => ", this.props);
    await AsyncStorage.removeItem("login");
    console.log("item login removed");
    navigate("Home") //something like that
    return ;
}

如何通过导航重定向到“主页”页面? 谢谢帮忙!

【问题讨论】:

  • 这个 removeItem 函数在哪里?
  • this.props 将在反应组件中可用。您在一个不扩展 Component 的函数中。

标签: react-native


【解决方案1】:

如果你使用react-navigation,我猜你使用createStackNavigator,所以你可以使用

export async function removeItem() {
    console.log("navigate => ", this.props);
    await AsyncStorage.removeItem("login");
    console.log("item login removed");

    this.props.navigation.navigate('Home') // redirect to home

    return ;
}

或者如果你想回到上一个屏幕,你可以使用pop

const popAction = StackActions.pop({
  n: 1, // number of screens to pop back by.
});

this.props.navigation.dispatch(popAction);

【讨论】:

  • 您好,我已经尝试过 this.props.navigation.navigte("Home") 它不起作用。我没有定义,我该如何解决这个问题?
  • 你在使用 react-navigation 吗?
  • 是的,但我是 react native 的新手
  • 好的,如果你使用react-navigation,你必须配置createStackNavigator,你这样做了吗?这是一个例子snack.expo.io/@react-navigation/going-back-v3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多