Skip to content

How to ensure the callback only executes when the watched value(s) change(s)? #12

@benface

Description

@benface

First of all, thank you so much for this library. 🤩 👏

But is it by design that the callback is called not just when one of the specified properties change, but also when the observer is initially attached to an element? If so, is there any way around that?

My use case is a React component that changes the state of one of its ancestors via context when a custom property changes inside it, but because the callback is called initially as well, it causes an infinite render loop.

Simple reproduction in CodeSandbox (basically the same example you have in the README but in a React/TS project because I had errors when trying in a vanilla JS project for some reason; see how values is logged on load)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthas workaroundThere is a workaround that allows one to achieve what is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions