【问题标题】:how to redirect from axios in react native如何在本机反应中从 axios 重定向
【发布时间】:2022-12-12 19:58:26
【问题描述】:

我正在尝试在 React Native 中重定向 我是 ReactNative 的新手 谁能帮我解决这个问题

 axios.post('http://10.0.2.2:8000/client/user', Data)

      .then(() => {
        //if succcess redirect to another url and pass data to that url
      })

      .catch(error => {
        alert(error);
      });
  }

我想要另一个网址并将数据传递到该 url 时。然后被执行

这是传递这个的正确方法吗

<PaperProvider>
        <NavigationContainer>
          <Stack.Navigator>
            <Stack.Screen
              options={{headerShown: false}}
              name="Home"
              component={HomeScreen}
            />
           
            <Stack.Screen name="Home_scrren" component={Home_scrren} />
            <Stack.Screen name="Pregnant" component={Pregnant} />
          
          </Stack.Navigator>
        </NavigationContainer>
      </PaperProvider>

【问题讨论】:

  • @aplha 我已经更新了我正在使用的问题堆栈
  • 所以在 api 调用之后你想导航到另一个屏幕并想调用另一个 api 对吗?
  • 如果 .then 被执行,我想导航到另一个屏幕并将数据传递到该屏幕

标签: reactjs react-native


【解决方案1】:

不用路由,你为什么不使用这样的东西navigation.navigate("ScreenName", {data: Data56})

这是示例:

      axios
        .post('http://10.0.2.2:8000/api/details', Data56)

        .then((res) => {
          navigation.navigate('ScreenName', { data: res.data });
        });

在屏幕组件上:


const Screen = ({
  navigation,
  route,
}) => {
  const data = route.params.data;

...

【讨论】:

    【解决方案2】:

    如果API 成功,这就是您可以导航的方式

    axios
      .post("http://10.0.2.2:8000/client/user", Data)
    
      .then((res) => {
        //if succcess redirect to another url and pass data to that url
        if (res.status == 200) {
          props.navigation.navigate("Pregnant", { data: res });
        } else {
          // show alert or your logic
        }
      })
    
      .catch((error) => {
        alert(error);
      });
    

    这是从另一个屏幕获取数据的方法: Read more on this

    useEffect(() => {
      console.log("route.params ==>", route.params);
    }, []);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2019-10-10
      • 2022-01-20
      • 1970-01-01
      • 2021-01-11
      相关资源
      最近更新 更多