'How to call api using fetch in React native

 const login: SubmitHandler<ILoginValues> = async ({email, password}) => {
    try {
      const res = await fetch(`${config.apiUrl}/api/login`, {
        method: 'POST',
        body: JSON.stringify({
          email,
          password,
        }),
      });
      if (res.ok) {
        await setGenericPassword(email, password, CREDENTIALS_STORAGE_OPTIONS);
        setUser({isLoggedIn: true, hasSessionExpired: false});
      }
    } catch (error) {
      toast.setToast({message: 'Login failed', visible: true});
    }
  };

I am creating a login flow in react native using java spring rest api. My Api is running at address http://localhost:8082/api/v1/users how can I get the data from client side using fetch in React native and also store the JWT token in client side.



Solution 1:[1]

You can perform the login request when the form is submitted. Than wait for the response and save the jwt in local storage. Than login the user into the logged in ui.

const form = document.getElementById("form-id")
form.addEventListener("submit", async (e) =>{ 
e.prevetDefault() your code })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivaylo