Edit tutorial

useOn() / useOnDocument() / useOnWindow()

Use useOn() / useOnDocument() / useOnWindow() to programmatically set up listeners on host elements. This is useful when you are creating custom APIs and don't have access to place these events in the JSX or if the events are not known ahead of time, such as if they are created based on component props.

Example

The example on the right shows how to use useOn() method. Duplicate the code to explore how to use useOnDocument() / useOnWindow() and how they behave differently.

Building preview

Refreshing App output

Compiling the latest client and SSR result for your current code.

No console activity yet

Interact with the app or trigger a render to see client and SSR messages here.