React – Hooks as Optional Parameters

You can put hooks as parameters, but should you? And why would you want to do that?

Here’s a Sandbox with me playing around:

https://codesandbox.io/s/hooks-as-params-1wt4bm

But basically, it’s like this:

function WeirdComponent({
hook …


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Bruno Noriller

You can put hooks as parameters, but should you? And why would you want to do that?

Here’s a Sandbox with me playing around:

https://codesandbox.io/s/hooks-as-params-1wt4bm

But basically, it’s like this:

function WeirdComponent({
    hook = useHook(),
    hook2 = useAnotherHook(hook),
}) {
    return (
        <div>
            <div>{hook}</div>
            <div>{hook2}</div>
        </div>
    )
}

function useHook(){
    return "hook!"
}

function useAnotherHook(value){
    return value || "default value"
}

And this will work exactly as if you would add the hooks inside the component.

But why would you do it?

Back when I realized it was possible, I used it as a way to simplify unit testing of some components.

This let me just pass the values I wanted without having to mock the hook.

Another surprising thing you can do is to inject a different hook depending on whatever you want and change a component compartment changing only the hook.

Should you?

Probably not.

Maybe there’s some niche use of this, but whatever you can do this way, you can do it in another way.

(If you have an idea for a use case of doing this I would love to know about it.)

Cover Photo by Grant Durr on Unsplash


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Bruno Noriller


Print Share Comment Cite Upload Translate Updates
APA

Bruno Noriller | Sciencx (2022-09-18T22:15:31+00:00) React – Hooks as Optional Parameters. Retrieved from https://www.scien.cx/2022/09/18/react-hooks-as-optional-parameters/

MLA
" » React – Hooks as Optional Parameters." Bruno Noriller | Sciencx - Sunday September 18, 2022, https://www.scien.cx/2022/09/18/react-hooks-as-optional-parameters/
HARVARD
Bruno Noriller | Sciencx Sunday September 18, 2022 » React – Hooks as Optional Parameters., viewed ,<https://www.scien.cx/2022/09/18/react-hooks-as-optional-parameters/>
VANCOUVER
Bruno Noriller | Sciencx - » React – Hooks as Optional Parameters. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/18/react-hooks-as-optional-parameters/
CHICAGO
" » React – Hooks as Optional Parameters." Bruno Noriller | Sciencx - Accessed . https://www.scien.cx/2022/09/18/react-hooks-as-optional-parameters/
IEEE
" » React – Hooks as Optional Parameters." Bruno Noriller | Sciencx [Online]. Available: https://www.scien.cx/2022/09/18/react-hooks-as-optional-parameters/. [Accessed: ]
rf:citation
» React – Hooks as Optional Parameters | Bruno Noriller | Sciencx | https://www.scien.cx/2022/09/18/react-hooks-as-optional-parameters/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.