You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is useful when you've just updated specific connection instances and don't want to invalidate them immediately, but want to invalidate all other instances.
274
+
251
275
### Common patterns
252
276
253
277
These functions work great together for common scenarios:
### Subscribing to invalidation state with `useSubscribeToInvalidationState`
315
+
316
+
When you invalidate records in the Relay store, queries won't automatically refetch until the next time they render. However, sometimes you need to take immediate action when specific records are invalidated, such as triggering an immediate refetch or updating some UI state.
317
+
318
+
For these scenarios, RescriptRelay provides `useSubscribeToInvalidationState` - a hook that lets you listen to invalidation events and react to them immediately.
319
+
320
+
#### Basic usage
321
+
322
+
```rescript
323
+
module UserProfile = %relay(`
324
+
fragment UserProfile_user on User {
325
+
__id
326
+
name
327
+
email
328
+
isOnline
329
+
}
330
+
`)
331
+
332
+
@react.component
333
+
let make = (~userRef) => {
334
+
let user = UserProfile.use(userRef)
335
+
let environment = RescriptRelay.useEnvironmentFromContext()
336
+
337
+
// Subscribe to invalidation of this specific user record
338
+
let _ = RescriptRelay.useSubscribeToInvalidationState([user.__id], () => {
339
+
// This callback will fire whenever the user record is invalidated
340
+
Console.log("User record was invalidated, taking action...")
341
+
342
+
// You could trigger a refetch, show a notification, etc.
You can listen to invalidation of multiple records at once:
396
+
397
+
```rescript
398
+
@react.component
399
+
let make = (~userIds: array<RescriptRelay.dataId>) => {
400
+
let environment = RescriptRelay.useEnvironmentFromContext()
401
+
402
+
// Listen to invalidation of any of these user records
403
+
let _ = RescriptRelay.useSubscribeToInvalidationState(userIds, () => {
404
+
// This will fire if any of the user records become invalidated
405
+
Console.log("One or more user records were invalidated")
406
+
407
+
// You might want to refetch a list or update some global state
408
+
})
409
+
410
+
// ... rest of component
411
+
}
412
+
```
413
+
414
+
#### When to use `useSubscribeToInvalidationState`
415
+
416
+
-**Real-time data updates**: When you need to immediately reflect that data has changed
417
+
-**Cache warming**: Preemptively fetching updated data before it's needed
418
+
-**UI state synchronization**: Updating component state that depends on the validity of cached data and where waiting for a re-render is not enough for the UX
419
+
-**Analytics/logging**: Tracking when specific data becomes stale
420
+
421
+
#### Integration with bulk invalidation
422
+
423
+
You can combine this with bulk invalidation patterns:
424
+
425
+
```rescript
426
+
// In a mutation updater
427
+
let invalidateUserConnections = (environment, userId, store) => {
428
+
let connectionIds = environment->RescriptRelay.Environment.findAllConnectionIds(
let unsubscribe = RescriptRelay.useSubscribeToInvalidationState(connectionIds, () => {
447
+
// React to any of the user's post connections being invalidated
448
+
Console.log("User's post connections were invalidated")
449
+
})
450
+
451
+
Some(unsubscribe)
452
+
}, [userId])
453
+
454
+
// ... rest of component
455
+
}
456
+
```
457
+
458
+
## Invalidation strategies and best practices
459
+
460
+
Understanding when and how to invalidate data is crucial for maintaining an optimal user experience. Here's a comprehensive guide to help you choose the right invalidation strategy.
461
+
462
+
### Choosing the right invalidation approach
463
+
464
+
#### 1. **Individual record invalidation** (`RecordProxy.invalidateRecord`)
465
+
466
+
**When to use:**
467
+
468
+
- Invalidating a specific entity after an update
469
+
- Simple scenarios where you know exactly which record changed
470
+
- When you want other cached data to remain valid
471
+
472
+
**Example:**
473
+
474
+
```rescript
475
+
// After updating a user's profile
476
+
UserUpdateMutation.commitMutation(
477
+
~environment,
478
+
~variables={userId, newName},
479
+
~updater=(store, response) => {
480
+
// Only invalidate the specific user that was updated
0 commit comments