Skip to content

How to implement Computed and Watchers functionality in lit #1193

Open
@rajeevnttdata

Description

@rajeevnttdata

Hello Team,

I just wanted to know how to achieve computed and watch functionality in the lit element.
Computed:- A computed property automatically tracks its reactive dependencies.
computed: {
// a computed getter
reversedMessage: function () {
// this points to the vm instance
return this.message.split('').reverse().join('')
}
}

Watchers:- Computed properties allow us to declaratively compute derived values. However, there are cases where we need to perform "side effects" in reaction to state changes - for example, mutating the DOM, or changing another piece of state based on the result of an async operation.

With Composition API, we can use them to trigger a callback whenever a piece of reactive state changes

const obj = reactive({ count: 0 })

watch(obj, (newValue, oldValue) => {
// fires on nested property mutations
// Note: newValue will be equal to oldValue here
// because they both point to the same object!
})

obj.count++

Developers if we can implement functionality Computed and Watchers. Just let me know ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions