t4t5 / nostr-react

React Hooks for Nostr 🦤
MIT License
85 stars 14 forks source link

Pass in dynamic callbacks #26

Open joshr4 opened 11 months ago

joshr4 commented 11 months ago

I was attempting to use the onEvent callback of the useNostrEvents hook, and ran into an issue where the callback cannot be updated. This was problematic, as my callback depended on my component's state, which changed over time. I successfully registered the callback, but as the callback was redefined on each render cycle, the useNostrEvents hook continued to use the initial callback that was referencing stale data.

The solution I came up with was to monitor the events array returned by useNostrEvents, and execute my callback on each new event that came in. Another approach that may work here is to allow the callback to be dynamically updated. This PR makes a change that may facilitate that.

I still need to test out this change to verify it is working as expected.