Forest.js Core Documentation - v1.2.3
    Preparing search index...

    Type Alias Store<T>

    Reactive store for state management. Stores a value and provides reactive updates. Used for managing dynamic data within Forest.js.

    const myStore = createStore({ value: "Hello" });
    myStore.set({ value: "World" });
    myStore.subscribe(() => console.log(myStore.get()));
    type Store<T> = {
        get: () => T;
        set: (value: T) => void;
        update: (fn: (prev: T) => T) => void;
        subscribe: (fn: () => void) => () => void;
        unsubscribe: (fn: () => void) => void;
    }

    Type Parameters

    • T

      Type of value stored in the store

    Index

    Properties

    get: () => T

    Get the current value from the store.

    Type declaration

      • (): T
      • Returns T

        The current store value.

    set: (value: T) => void

    Set a new value in the store and notify subscribers.

    Type declaration

      • (value: T): void
      • Parameters

        • value: T

          The new value to store.

        Returns void

    update: (fn: (prev: T) => T) => void

    Update the store value using a function and notify subscribers.

    Type declaration

      • (fn: (prev: T) => T): void
      • Parameters

        • fn: (prev: T) => T

          A function that takes the previous value and returns the new value.

        Returns void

    subscribe: (fn: () => void) => () => void

    Subscribe to store changes.

    Type declaration

      • (fn: () => void): () => void
      • Parameters

        • fn: () => void

          The callback function to execute on updates.

        Returns () => void

        A function to unsubscribe from changes.

    const unsubscribe = myStore.subscribe(() => console.log("Updated!"));
    unsubscribe(); // To stop listening
    unsubscribe: (fn: () => void) => void

    Unsubscribe a previously registered callback. Typically called using the function returned from subscribe().

    Type declaration

      • (fn: () => void): void
      • Parameters

        • fn: () => void

          The function that was initially registered.

        Returns void