Приклади вживання Custom hooks Англійська мовою та їх переклад на Українською
{-}
-
Colloquial
-
Ecclesiastic
-
Computer
Custom Hooks are more of a convention than a feature.
Do I have to name my custom Hooks starting with“use”? Please do.
Custom Hooks let you do this, but without adding more components to your tree.
It's most valuable for custom Hooks that are part of shared libraries.
Custom Hooks offer the flexibility of sharing logic that wasn't possible in React components before.
We are excited to see what custom Hooks the React community will come up with.
Custom Hooks are a convention that naturally follows from the design of Hooks, rather than a React feature.
UseDebugValue can be used to display a label for custom hooks in React DevTools.
Call Hooks from custom Hooks(we will learn about them on the next page).
(There is just one other valid place to call Hooks- your own custom Hooks. We will learn about them in a moment.).
Importantly, custom Hooks give you the power to constrain React API if you would like to type them more strictly in some way.
Thanks to the Rules of Hooks, we know that Hooks are only called from React components(or custom Hooks- which are also only called from React components).
Custom Hooks let you combine Hooks provided by React into your own abstractions, and reuse common stateful logic between different components.
You can learn more about custom Hooks on a dedicated page: Building Your Own Hooks. .
Custom Hooks are a mechanism to reuse stateful logic(such as setting up a subscription and remembering the current value), but every time you use a custom Hook, all state and effects inside of it are fully isolated.
You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven't considered.
You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven't considered.
How does a custom Hook get isolated state?
We don't recommend adding debug values to every custom Hook.
Now let's see how we can use our custom Hook.
A custom Hook is a JavaScript function whose name starts with”use” and that may call other Hooks. .
If you need to test a custom Hook, you can do so by creating a component in your test, and using your Hook from it.
For example a custom Hook that returned a Date value could avoid calling the toDateString function unnecessarily by passing the following formatter:.
But we also encourage you to start spotting cases where a custom Hook could hide complex logic behind a simple interface, or help untangle a messy component.
This might be a bit convoluted butyou can extract it into a custom Hook:.
It makes it easy to later extract some related logic into a custom Hook, for example:.
Unlike a React component, a custom Hook doesn't need to have a specific signature.
If the state logic becomes complex,we recommend managing it with a reducer or a custom Hook.
Note how we were able to move the useState call for the position state variable andthe related effect into a custom Hook without changing their code.
(If you find yourself doing this often, you could create a custom Hook for it.).