Code Snippets

A curated collection of useful code snippets for everyday development

Typescript
9/16/2025

ewjdajewew dup dup dupp

oiewjdojewjdweiu

Loading...

import { useState, useEffect, useCallback } from 'react';

interface UseApiState { data: T | null; loading: boolean; error: string | null; }

export function useApi(url: string, options?: RequestInit) { const [state, setState] = useState>({ data: null, loading: true, error: null, });

const fetchData = useCallback(async () => { try { setState(prev => ({ ...prev, loading: true, error: null })); const response = await fetch(url, options); if (!response.ok) { throw new Error(HTTP error! status: ${response.status}); } const data = await response.json(); setState({ data, loading: false, error: null }); } catch (error) { setState({ data: null, loading: false, error: error instanceof Error ? error.message : 'An error occurred', }); } }, [url, options]);

useEffect(() => { fetchData(); }, [fetchData]);

return { ...state, refetch: fetchData }; }

Typescript
9/16/2025

ewjdajewew dup dup

oiewjdojewjdweiu

Loading...

import { useState, useEffect, useCallback } from 'react';

interface UseApiState { data: T | null; loading: boolean; error: string | null; }

export function useApi(url: string, options?: RequestInit) { const [state, setState] = useState>({ data: null, loading: true, error: null, });

const fetchData = useCallback(async () => { try { setState(prev => ({ ...prev, loading: true, error: null })); const response = await fetch(url, options); if (!response.ok) { throw new Error(HTTP error! status: ${response.status}); } const data = await response.json(); setState({ data, loading: false, error: null }); } catch (error) { setState({ data: null, loading: false, error: error instanceof Error ? error.message : 'An error occurred', }); } }, [url, options]);

useEffect(() => { fetchData(); }, [fetchData]);

return { ...state, refetch: fetchData }; }

Typescript
9/16/2025

ewjdajewew dup

oiewjdojewjdweiu

Loading...

import { useState, useEffect, useCallback } from 'react';

interface UseApiState { data: T | null; loading: boolean; error: string | null; }

export function useApi(url: string, options?: RequestInit) { const [state, setState] = useState>({ data: null, loading: true, error: null, });

const fetchData = useCallback(async () => { try { setState(prev => ({ ...prev, loading: true, error: null })); const response = await fetch(url, options); if (!response.ok) { throw new Error(HTTP error! status: ${response.status}); } const data = await response.json(); setState({ data, loading: false, error: null }); } catch (error) { setState({ data: null, loading: false, error: error instanceof Error ? error.message : 'An error occurred', }); } }, [url, options]);

useEffect(() => { fetchData(); }, [fetchData]);

return { ...state, refetch: fetchData }; }

Typescript
9/16/2025

ewjdajewew

oiewjdojewjdweiu

Loading...

import { useState, useEffect, useCallback } from 'react';

interface UseApiState { data: T | null; loading: boolean; error: string | null; }

export function useApi(url: string, options?: RequestInit) { const [state, setState] = useState>({ data: null, loading: true, error: null, });

const fetchData = useCallback(async () => { try { setState(prev => ({ ...prev, loading: true, error: null })); const response = await fetch(url, options); if (!response.ok) { throw new Error(HTTP error! status: ${response.status}); } const data = await response.json(); setState({ data, loading: false, error: null }); } catch (error) { setState({ data: null, loading: false, error: error instanceof Error ? error.message : 'An error occurred', }); } }, [url, options]);

useEffect(() => { fetchData(); }, [fetchData]);

return { ...state, refetch: fetchData }; }