In my experience building custom CRM cards and UI extensions for HubSpot, there isn’t currently a native React hook specifically for listening to association changes in real-time within the UI. This is a gap that many developers have encountered.
However, here are several approaches you can use to achieve similar functionality:
Polling with Associations API
You can set up interval-based polling in your React component using the HubSpot Associations API. Use useEffect with setInterval to periodically check for new associations. While not as elegant as a hook, it works reliably for most use cases. Set the polling interval based on your performance needs, typically 5-10 seconds works well without causing performance issues.
Combine with Browser Events
Listen for DOM mutation events or use a MutationObserver to detect when the associations section of the Deal UI updates. When you detect changes in the DOM, trigger a fetch to the Associations API to get the updated data. This approach is more responsive than pure polling but requires careful implementation to avoid memory leaks.
Webhook + Server-Sent Events
Since you mentioned already having webhook capability on the backend, you could enhance this by implementing Server-Sent Events or WebSockets. Your backend receives the webhook from HubSpot when an association is created, then pushes that update to your frontend in real-time. This gives you the immediate UI update you’re looking for.
Custom Event Dispatcher
Create a custom event dispatcher that your card listens to. When users create Notes or other activities, those actions often trigger other UI updates. You can intercept these using event delegation and manually trigger your card refresh.
Use the CRM Record Update Hook
While not specifically for associations, you can use the onCrmRecordUpdate hook and check if the lastModifiedDate has changed. When it does, fetch the associations to see if they’ve been updated. This isn’t perfect but can catch many association changes.
Implementation Example:
For the polling approach, you’d structure it something like:
useEffect(() => {
const checkAssociations = async () => {
// Fetch associations using HubSpot API
const associations = await fetchAssociations(dealId);
setCardData(associations);
};
const interval = setInterval(checkAssociations, 8000);
return () => clearInterval(interval);
}, [dealId]);
Have you considered submitting this as a feature request to HubSpot? An onAssociationChange hook would be valuable for the developer community. In the meantime, the polling + webhook hybrid approach tends to give the best balance of responsiveness and reliability.