Skip to content

Instantly share code, notes, and snippets.

@vijayjangid
Last active September 29, 2023 05:51
Show Gist options
  • Save vijayjangid/00206eb9c96f920cdb219a90765a75ba to your computer and use it in GitHub Desktop.
Save vijayjangid/00206eb9c96f920cdb219a90765a75ba to your computer and use it in GitHub Desktop.
React Fishing Hook

Given a Pond (array of Fishes) where the Fish object is as below:

// Fish
{
name: string,
size: 'small'|'medium'|'large'
hungry: boolean
}

const pond = [
  { name: 'Nemo', size: 'small', hungry: true },
  { name: 'Dory', size: 'medium', hungry: false },
  { name: 'Bubbles', size: 'large', hungry: true },
  { name: 'Fin', size: 'medium', hungry: true },
  { name: 'Marlin', size: 'large', hungry: false },
  { name: 'Gill', size: 'small', hungry: true },
  { name: 'Bruce', size: 'medium', hungry: true },
  { name: 'Squirt', size: 'small', hungry: false },
  { name: 'Hank', size: 'large', hungry: true },
  { name: 'Bailey', size: 'medium', hungry: false }
];

Create a custom React hook "useFishing" which when called, creates a wave in Pond and do the following:

  1. Catch a random fish
  2. If the fish is not hungry, then the fish skips the hook and returns [].
  3. If the fish is hungry, return the fish and all the fishes which are bigger in size to the caught fish and are hungry. (Analogy is the bigger hungry fishes try to catch this fellow and get trapped too) returns Fish[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment