Skip to main content

Command Palette

Search for a command to run...

React Native Custom Hooks - Part 1

Updated
2 min read
React Native Custom Hooks - Part 1
A

Experienced mobile app developer who has a track record of success creating apps that are both well-received and commercially viable. Skilled with working as a team and incorporating input into projects. Ability to always look for ways to improve upon an already existing app to keep people downloading it and enjoying it. Strong eye for detail and tenacity to never quit on something until it is absolutely perfect.

- useEffectOnce

import { useEffect } from 'react';

export function useEffectOnce(cb) {
  useEffect(cb, []);
}

Example

import { useEffectOnce } from './hooks';

useEffectOnce(() => {
  console.log('Will be executed at first render only');
});

- useUpdateEffect

import { useEffect, useRef } from 'react';

export function useUpdateEffect(callback, dependencies) {
  const firstRenderRef = useRef(true);

  useEffect(() => {
    if (firstRenderRef.current) {
      firstRenderRef.current = false;
      return;
    }
    return callback();
  }, dependencies);
}

Example

import { useState } from 'react';
import { useUpdateEffect } from './hooks';

const [counter, setCounter] = useState(5);

useUpdateEffect(() => {
  console.log('Will be executed whenever the dependency updates, But won\'t be executed at first render');
}, [counter]);

- useToggle

export function useToggle(defaultValue = false) {
  const [value, setValue] = useState(defaultValue);

  const toggleValue = useCallback(() => setValue((value) => !value), []);

  return [value, toggleValue];
}

Example

import { useToggle } from './hooks';

const [isActive, setIsActive] = useToggle(false);

- useTimeout

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

export function useTimeout(callback, delay) {
  const callbackRef = useRef(callback);
  const timeoutRef = useRef(null);

  useEffect(() => {
    callbackRef.current = callback;
  }, [callback]);

  const set = useCallback(() => {
    timeoutRef.current = setTimeout(() => callbackRef.current(), delay);
  }, [delay]);

  const clear = useCallback(() => {
    timeoutRef.current && clearTimeout(timeoutRef.current);
  }, []);

  useEffect(() => {
    set();
    return clear;
  }, [delay, set, clear]);

  const reset = useCallback(() => {
    clear();
    set();
  }, [clear, set]);

  return { reset, clear };
}

Example

import { useState } from 'react';
import { useTimeout } from './hooks';

const [counter, setCounter] = useState(5);
const { clear, reset } = useTimeout(() => setCounter(0), 1000);

// counter value is 5, and after 1000ms the value will be changed to 0 as we can see, and we also have 2 functions, clear that clears Timeout, and a Reset function.

- useDebounce

import { useEffect } from 'react';
import { useTimeout } from './useTimeout';

export function useDebounce(callback, delay, dependencies) {
  const { reset, clear } = useTimeout(callback, delay);
  useEffect(reset, [...dependencies, reset]);
  useEffect(clear, []);
}

Example

import { useState } from 'react';
import { useDebounce } from './hooks';

const [term, setTerm] = useState('');
useDebounce(async () => await searchAutoComplete(term), 1000, [term]);

// Typing in a search field, and we want to send a search auto complete request that returns array of auto complete words, but we want to send this request only after user stops typing for 1000ms

D

Another great and clear article! Anas Nabil

2
A

Thank you!

More from this blog

Anas Nabil's Blog

16 posts