DEV Community

Breno Novelli
Breno Novelli

Posted on

3 1

useIpAdrress()

useIpAddress.ts

import axios from 'axios';
import { useCallback } from 'react';

/**
 * Hook para retornar o IP do usuário.
 *
 * @example
 * const [ipAddress] = useIpAddress();
 * ip_address: await ipAddress(),
 *
 * @return String com o endereço de IP do usuário
 */

const useIpAddress = () => {
  const ipAddress = useCallback(async () => {
    const { data } = await axios.get('https://ipv4.icanhazip.com');

    return data;
  }, []);

  return [ipAddress];
};

export { useIpAddress };

Enter fullscreen mode Exit fullscreen mode

Component.tsx

import { useIpAddress } from 'hooks/useIpAddress';

const [ipAddress] = useIpAddress();

const data = { ip_address: await ipAddress(), ...rest};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series