Function volatileProp

  • Creates a volatile property accessor on a target object. The property is considered "volatile" because it does not persist on the object itself and is not part of its persisted data. Note: Volatile props for unique nodes (nodes with a same type and key) will be shared, since they are actually always the same instance.

    Type Parameters

    • TTarget extends object

      The type of the target object.

    • TValue

      The type of the volatile property's value.

    Parameters

    • defaultValueGen: () => TValue

      A function that returns the default value for the property.

    Returns VolatileProp<TTarget, TValue>

    A tuple where the first element is the getter function, the second is the setter function and the third is the reset function to set a default value again.

    const [getVolatile, setVolatile, resetVolatile] = volatileProp(() => 0);

    const obj = node({});

    // Initially, the volatile property is 0.
    console.log(getVolatile(obj)); // outputs 0

    // Update the volatile property:
    setVolatile(obj, 42);
    console.log(getVolatile(obj)); // outputs 42

    // Reset the volatile property:
    resetVolatile(obj);
    console.log(getVolatile(obj)); // outputs 0