Skip to content

useContractEvents hook persists old event data when event filter changes #7737

@alecananian

Description

@alecananian

The query key for the useContractEvents hook uses a hash based on an event's signature without taking into account any filters applied to it, so if the filters change, the hook continues to return old data prepended to the new event data.

https://github.com/thirdweb-dev/js/blob/main/packages/thirdweb/src/react/core/hooks/contract/useContractEvents.ts#L91

Proposed fix is to add the encoded topics to the query key:

const eventsKey = useMemo(
  () =>
    events?.reduce((acc, curr) => {
      // we can use the event hash as a unique identifier?
      return `${acc}${curr.hash}${curr.topics.join()}_`;
    }, "") || "__all__",
  [events],
);

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions