Home > Net >  how to redirect from axios in react native
how to redirect from axios in react native

Time:12-12

i am trying to redirect in react native am new to reactnative can anyone help me with this

 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);
      });
  }

i want to another to a url and pass data to that url when .then is executed

is this the correct method to pass this

<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>

CodePudding user response:

This is how you can navigate if API is successful

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.data });
    } else {
      // show alert or your logic
    }
  })

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

And this is how you can get data from the another screen: Read more on this

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