This Pattern Will Make Your React Hooks Cleaner | by Ahmad M. Hawwash | Mar, 2022

So that you don’t need to redo work

Fish hooks
How Hooks Are BuiltCan We Combine the OptionsWhy Does That WorkConclusion

After we construct a customized hook with advanced logic in React, we both return an array or an object that has state and logic. The next exhibits the method:

1. Returning an object

The primary choice is to return an object from the customized hook, like this:

const customHook = (configs) => 
const isLoading = true //logic
const reload = () => /*logic*/
return isLoading, reload

The utilization would appear to be this:

const  isLoading, reload  = customHook(configs)

The place:

  • It’s developer-friendly, the place you don’t want to recollect the order of the props
  • The IntelliSense will assist you already know the params

However after we use multiples of that hook, it doesn’t look good. It can appear to be this:

isLoading: isLoadingWidget1,
reload: reloadWidget1

= customHook(widget1Config)
isLoading: isLoadingWidget2,
reload: reloadWidget2

= customHook(widget2Config)
isLoading: isLoadingWidget3,
reload: reloadWidget3

= customHook(widget3Config)

Which isn’t very nice. We’ve got to rename every key to a different one so that they don’t conflict.

2. Returning an array

The opposite choice is to return an array from the customized hook, like the next:

const customHook = (configs) => 
const isLoading = true //logic
const reload = () => /*logic*/
return [isLoading, reload]

The utilization would appear to be this:

const [isLoading, reload] = customHook(configs)

The place:

  • It solves the a number of usages drawback. You don’t need to rename every key


  • You need to preserve the order of props
  • Doesn’t have the identical IntelliSense as in Objects

Can we mix these patterns collectively? In a way, if you wish to use it as an array or as an object for a similar hook.

Sure, that’s attainable. Take a look at this implementation:

const customHook = () =>   
const isLoading = true //logic
const reload = () => /*logic*/
const end result = [isLoading, reload] end result.isLoading = isLoading
end result.reload = reload
return end result

end result is an array, but we add isLoading an reload keys to it.

That implementation will give us the power to make use of that hook as:

  1. An object
const  isLoading, reload  = customHook(configs)

2. An array

const [isLoading, reload] = customHook(configs)

Each of those choices will work. However why…

Why is it attainable to make use of each the Array and Object return sorts? Why is it attainable to connect keys to an array?

“The Array object, as with arrays in different programming languages, permits storing a collection of multiple items under a single variable name, and has members for performing common array operations.” — MDN

As talked about, “The Array object.” That reveals the truth that an array is definitely simply an object, with a particular constructor, and a set of strategies performing widespread array operations.

Proof that Array is definitely an Object

  1. Proof 1
typeof [] // 'object'

Sure, typeof will end in an 'object'

2. Proof 2

const 0: first, 1: second = [1, 2, 3, 4] // first: 1, second: 2

We are able to destruct the array object utilizing the indices as a result of the keys in any array are simply the indices and values of the thing, and they’re what you go between the brackets []. That’s why we’re capable of destruct it and rename keys that manner.

3. Proof 3

const  size  = [1, 2, 3, 4]

An array is an object that additionally has size key in it.

4. Proof 4

Object.keys([1, 2, 3, 4])   // ['0', '1', '2', '3']
Object.values([1, 2, 3, 4]) // [1, 2, 3, 4]

Object.keys and Object.values solely work on Objects, and Array is an object (a particular object).

We’ve got simply seen a sample that permits us to make hooks’ return worth to be destructed as an array or as an object. We’ve seen how that’s useful. And we’ve seen why that’s attainable as a result of it’s all about the truth that Arrays are simply Objects with a cap.


More Posts