use_callback

use_callback is a hook that memoizes a callback function, returning the same callback function on subsequent renders when the dependencies have not changed. This is useful when passing callbacks to functions or components that rely on reference equality to prevent unnecessary re-renders or effects from firing.

Example

from deephaven import ui
import time


@ui.component
def ui_server():
    theme, set_theme = ui.use_state("red")

    create_server = ui.use_callback(lambda: {"host": "localhost"}, [])

    def connect():
        server = create_server()
        print(f"Connecting to {server}")
        time.sleep(0.5)

    ui.use_effect(connect, [create_server])

    return ui.view(
        ui.picker(
            "red",
            "orange",
            "yellow",
            label="Theme",
            selected_key=theme,
            on_change=set_theme,
        ),
        padding="size-100",
        background_color=theme,
    )


my_server = ui_server()

In the example above, use_callback memoizes the create_server callback. The connect function is then passed to use_effect with create_server as a dependency. This ensures the effect will not be triggered on every re-render because the create_server callback is memoized.

use_callback is similar to use_memo, but for functions instead of values. Use use_callback when you need to memoize a callback function that relies on reference equality to prevent unnecessary re-renders.

Recommendations

Recommendations for memoizing callback functions:

  1. Use memoization when callbacks are passed into expensive effects: If the callback is being passed into an expensive use_effect or use_memo call, use use_callback so that it maintains referential equality.
  2. Use dependencies: Pass in only the dependencies that the memoized callback relies on. If any of the dependencies change, the memoized callback will be re-computed.

API Reference

Memoize a callback function. The callback will only be recreated if the dependencies change.

Returns: TypeVar(T, bound= Callable[..., Any]) The memoized callback function.

ParametersTypeDefaultDescription
funcTypeVar(T, bound= Callable[..., Any])The function to create a memoized callback for.
dependenciesTuple[Any] |
List[Any]
The dependencies to check for changes.