Skip to main content

Lesson 8 of 9

Mini Project: Custom Hook Library

Build a reusable hook library for a React Native app - the shared/hooks/ folder from the feature-based architecture. Each hook is production-ready with proper TypeScript types, cleanup, and error handling.

45 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

useGeolocation Hook

useGeolocation Hook
import { useState, useEffect } from 'react';
import * as Location from 'expo-location';

interface GeoState {
  location: Location.LocationObject | null;
  error: string | null;
  isLoading: boolean;
  permissionStatus: Location.PermissionStatus | null;
}

export function useGeolocation(options?: Location.LocationOptions) {
  const [state, setState] = useState<GeoState>({
    location: null, error: null, isLoading: true, permissionStatus: null,
  });

  useEffect(() => {
    let subscription: Location.LocationSubscription | null = null;

    (async () => {
      // 1. Request permission
      const { status } = await Location.requestForegroundPermissionsAsync();
      setState(s => ({ ...s, permissionStatus: status }));

      if (status !== 'granted') {
        setState(s => ({ ...s, error: 'Location permission denied', isLoading: false }));
        return;
      }

      // 2. Get current position
      const location = await Location.getCurrentPositionAsync(options ?? {});
      setState(s => ({ ...s, location, isLoading: false }));

      // 3. Watch for updates
      subscription = await Location.watchPositionAsync(
        options ?? { accuracy: Location.Accuracy.Balanced },
        loc => setState(s => ({ ...s, location: loc }))
      );
    })().catch(err => setState(s => ({ ...s, error: err.message, isLoading: false })));

    return () => { subscription?.remove(); };
  }, []);

  return state;
}

// ---- useHaptics - tactile feedback ----
import * as Haptics from 'expo-haptics';

export function useHaptics() {
  return {
    impact: (style: Haptics.ImpactFeedbackStyle = Haptics.ImpactFeedbackStyle.Medium) =>
      Haptics.impactAsync(style),
    notification: (type: Haptics.NotificationFeedbackType = Haptics.NotificationFeedbackType.Success) =>
      Haptics.notificationAsync(type),
    selection: () => Haptics.selectionAsync(),
  };
}

// Usage
function LikeButton({ postId }: { postId: string }) {
  const [liked, setLiked] = useState(false);
  const { impact } = useHaptics();

  function handlePress() {
    impact(Haptics.ImpactFeedbackStyle.Light); // tactile feedback
    setLiked(l => !l);
  }

  return (
    <Pressable onPress={handlePress} accessibilityRole="button" accessibilityLabel={liked ? 'Unlike post' : 'Like post'}>
      <Text style={{ fontSize: 24 }}>{liked ? '❤️' : '🤍'}</Text>
    </Pressable>
  );
}

Tip

Tip

Practice Mini Project Custom Hook Library in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Suite of reusable mobile utility hooks for network, debounce, and dimensions

Practice Task

Note

Practice Task - (1) Write a working example of Mini Project Custom Hook Library from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Mini Project Custom Hook Library is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/9 done