Hook

abstract class Hook<C, R, P> : Property<Effect<C, R, P>>

A Hook is a special kind of Property that encapsulates an Effect as the Property's value.

A hook enables a client to define some custom behaviour that the applicator of the hook (a component for most cases) should apply. The behaviour is often closely tied to some data provided by the client. In fact, in most cases those data is needed to configure the Effect, which is usually done in appropriate operator fun invoke methods.

So, the client has to configure the values defined by the property portion and the applicator applies the effect within its own UI context C.

The applicator can pass additional data into the effect as payload P (think of some `close` handler passed into the apply-expression to enable the client to create some custom close-button for example).

The applicator can easily execute the Hook by calling one of the hook methods, which basically simply pass the payload into the Effect and executes it. Finally an optional alsoExpr has to be applied to the return value R in the different variations of the Effect.

If an implementation needs some complex payload, use some dataclass or other containers to comply to its signature.

DO NOT CREATE SOME OTHER HOOK ABSTRACTION WITH MULTIPLE TYPES FOR THE PAYLOAD! (So please NO Hook<C, R, P1, P2, ...>!!!)

For the special cases where an applicator knows about the specific internal rendering structure of a hook implementation, prefer to use TagHook or apply the special payload type TagPayload by yourself.

There exist special hook methods for this type alias, which offers a nice convenience API for the hook applicator to directly pass the styling and id parameters additionally to the generic payload P.

Components should strive to use hooks for all their parts that need to be configurable and varying in their behaviours. Think of some input-field that should render a label next to it. This label clearly needs to be configured by the calling client, so it must be configurable. The label could be reused for different kind of form control elements too, so it clearly makes sense to encapsulate the configuration and its representation (the rendering function) into one object that should be some kind of hook.

See also

Inheritors

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
var alsoExpr: R.() -> Unit?

This field encapsulates some behaviour, which should be applied to the result R of the Hook's Effect.

Link copied to clipboard
Link copied to clipboard
open var value: Effect<C, R, P>?

Functions

Link copied to clipboard
fun also(expr: R.() -> Unit)

This method offers the created result as receiver within a context expression in order to expose some additional configuration entry point for a client.

Link copied to clipboard
fun use(item: Effect<C, R, P>)