Custom hooks
Custom hooks allow you to encapsulate reusable logic within your Kiru components. This documentation outlines the steps and considerations for building your own custom hooks.
Understanding the Code:
The provided code snippet showcases the useHook function, which is the foundation for creating hooks in Kiru. Here's a breakdown of its functionality:
-
function:
-
Takes three arguments:
hookName: A string representing the name of your custom hook.hookData: An object containing data specific to your hook's logic.callback: A function that defines the behavior of your custom hook. It receives an object containing information about the current execution context and can access the providedhookData.
-
Retrieves the Kiru virtual node (vNode) associated with the component.
-
Validates that the hook is called at an appropriate location (top level of a component or within another hook).
-
Extracts relevant context information from the vNode.
-
Calls the provided
callbackfunction with details about the current state and methods for updating the component or scheduling effects. -
Stores the used
hookDatawithin the vNode for future references. -
Returns the result returned by the
callbackfunction.
-
Creating a Custom Hook:
-
Naming: Choose a descriptive name starting with
use(e.g.,useAuth,useWindowSize). -
hookData: Define the data structure your hook will use. This can be an object containing state variables, functions, or other necessary information. -
function: This function implements the core logic of your custom hook. It receives an object containing details about the current execution context, including:
hook: The currenthookDataobject.isInit: A boolean indicating whether the hook is being called for the first time.update: A function to trigger a re-render of the component.queueEffect: A function to schedule side effects within your hook.vNode: The Kiru virtual node associated with the component.
-
Return value: The
callbackfunction should return the value(s) you want to expose to your component's logic.
Example (simplified, adapted from useState):
function useCounter(initialValue = 0) {
return useHook(
"useCounter",
{ count: initialValue },
({ hook, update }) => {
const increment = () => {
hook.count++
update()
}
const decrement = () => {
hook.count--
update()
}
return [hook.count, increment, decrement]
}
)
}Key Considerations:
- Accessing previous state (optional): The
isInitargument in the callback allows you to determine whether the hook is being called for the first time. - Triggering re-renders: Use the provided
updatefunction to trigger a re-render of the component when your hook's state changes. - Scheduling effects: Use the
queueEffectfunction to schedule side effects that are executed post-update within your custom hook. sideEffectsEnabledfunction (optional): Kiru exposes asideEffectsEnabledfunction that allows you to determine whether side effects (eg. DOM interactions) should be executed.sideEffectsEnabledwill returnfalseif Kiru's currentrenderModeis set tostringorstream.
By following these guidelines, you can create custom hooks that promote code reusability, improve component organization, and enhance the maintainability of your Kiru applications.