Custom Hooks
If any TNG hooks are used in a non-Articulated Function, it behaves essentially like a React "Custom Hook". A TNG Custom Hook must be called, directly or indirectly, from an Articulated Function, so that it has an active TNG hooks-context available to use.
For example:
Note: Unlike React, TNG does not require naming Custom Hooks in the format useWHATEVER(..)
with a use
prefix. You can do so if you prefer, as we did in the above snippet. See the rules of TNG hooks below.
The useHitCounter(..)
Custom Hook -- again, just a normal non-Articulated Function that uses a TNG hook like useState(..)
! -- inherits the TNG hooks-context of the Articulated Function that invoked it. In this example, the invoking Articulated Function is either one of the two click handlers (produced via the two TNG(..)
calls) that were bound, respectively, as each button's click handler.
In other words, the line var [count,updateCount] = useState(0);
acts as if it had actually been called inside of one of the click handlers, even though it's in the separate useHitCounter(..)
function; that's what makes useHitCounter(..)
a Custom Hook, meaning it can be called from any Articulated Function.