Home > @theatre/core > onChange

onChange() function

Calls callback every time the pointed value of pointer changes.

Signature:

export declare function onChange<P extends PointerType<$IntentionalAny>>(pointer: P, callback: (value: P extends PointerType<infer T> ? T : unknown) => void): VoidFn;

Parameters

Parameter Type Description
pointer P A Pointer (like object.props.x)
callback (value: P extends PointerType<infer T> ? T : unknown) => void The callback is called every time the value of pointer changes

Returns:

VoidFn

An unsubscribe function

Example

Usage:

import {getProject, onChange} from '@theatre/core'

const obj = getProject("A project").sheet("Scene").object("Box", {position: {x: 0}})

const usubscribe = onChange(obj.props.position.x, (x) => {
  console.log('position.x changed to:', x)
})

setTimeout(usubscribe, 10000) // stop listening to changes after 10 seconds