Skip to content
0

katanakit-js / useCreateEffect

Function: useCreateEffect() ​

ts
function useCreateEffect(callback, signals?): () => void;

Defined in: src/core/services/reactive.service.ts:114

Creates a side effect that re-runs whenever a signal read by the callback changes. Explicit dependencies can also be provided for compatibility. An optional cleanup function returned from the callback is invoked before each re-run and on disposal.

Parameters ​

ParameterTypeDefault valueDescription
callback() => void | (() => void)undefinedEffect body. May return a cleanup function.
signalsSubscribable<unknown>[][]Optional explicit dependencies, in addition to tracked reads.

Returns ​

A dispose function that tears down all subscriptions and runs the final cleanup.

() => void

Example ​

ts
const [name, setName] = useCreateSignal("world");
const dispose = useCreateEffect(() => {
  console.log(`Hello, ${name()}!`);
  return () => console.log("cleaning up");
});

setName("Katana"); // logs "Hello, Katana!"
dispose();         // logs "cleaning up"

Released under the MIT License.