Skip to content

observe

The observe function allows you to create observers that react to changes in ref objects, providing a callback that triggers when the ref’s value changes. This is useful for handling side effects or updating UI elements based on reactive data.

Try It Online

To create an observer, you can use the observe function, passing in a ref object and a callback function.

import { observe, ref } from 'regor'
const myRef = ref('Hello, Regor!')
// Create an observer
const stopObserving = observe(myRef, (newValue) => {
console.log('Value changed:', newValue)
})
  • source: The ref object you want to observe for changes.
  • observer: A callback function that will be triggered whenever the ref’s value changes.
  • init (optional): If true, the observer will be immediately invoked with the current value of the ref when it’s created.
  • The observe function returns a stopObserving function that allows you to stop the observer when it’s no longer needed. Calling stopObserving will unsubscribe the observer from the ref object.
import { observe, ref } from 'regor'
const myRef = ref('Hello, Regor!')
// Create an observer with init set to true
const stopObserving = observe(
myRef,
(newValue) => {
console.log('Value changed:', newValue)
},
true,
)
// Later, when the observer is no longer needed, stop it
stopObserving()

Back to the API list