Core API Overview > @adpt/core > useMethod
useMethod() function
This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
Call an instance method on the Element that hand
refers to.
Signature:
export declare function useMethod<H extends Handle, Instance = HandleInstanceType<H>, MethodName extends MethodNames<Instance> = MethodNames<Instance>, Ret = ReturnTypeOrNever<Instance[MethodName]>>(hand: H | null, method: MethodName): Ret | undefined;
Parameters
Parameter | Type | Description |
---|---|---|
hand | H | null | The handle for the element upon which to call the method method . hand may also be null , in which case, initial is always the return value and the other arguments are ignored. |
method | MethodName | Name of the instance method to call. |
Returns:
Ret | undefined
Remarks
This hook is the primary way for a function component to call an instance method on another component element. A hook is used in order to delay execution of the method until the DOM is completely built. The reason this delayed execution is needed is because during the DOM build process, the element that hand
refers to may not have been built yet, or hand
may change to point to a different element later in the build process. By waiting until this avoids element build order issues and ensures that handle references are no longer changing.
Because execution of the methods is delayed, useMethod
will always return the initial
value on the initial build of a component. After every DOM build is complete, the method will be invoked during the state update phase and the return value stored in the component's state. This state update (or any state update) will cause the DOM to build again. Upon rebuild, the value stored from the last method invocation in the component's state will be returned and a new invocation will be queued.
If the value returned by the called method continues to change, this will cause the DOM to continue to be rebuilt again.
As this is a hook, it **must not** be called conditionally by a component. In cases where a handle is not always present or the method should not be called, call useMethod
with null
for hand
.