When possible, you'll want to favor Resource over LifecycleResource as Resource is simpler.
They key differences are that the LifecycleResource base class has 3 lifecycle hooks
setup - called upon first access of the resource
update - called when any tracked used during setup changes
teardown - called when the containing context is torn down
The main advantage to the LifecycleResource is that the teardown hook is for "last teardown",
whereas with Resource, if a destructor is registered in the destructor, there is no way to know
if that destruction is the final destruction.
An example of when you'd want to reach for the LifecycleResource is when you're managing external long-lived
state that needs a final destruction call, such as with XState, which requires that the "State machine interpreter"
is stopped when you are discarding the parent context (such as a component).
When possible, you'll want to favor
ResourceoverLifecycleResourceasResourceis simpler.They key differences are that the
LifecycleResourcebase class has 3 lifecycle hookssetup- called upon first access of the resourceupdate- called when anytrackedused duringsetupchangesteardown- called when the containing context is torn downThe main advantage to the
LifecycleResourceis that the teardown hook is for "last teardown", whereas withResource, if a destructor is registered in the destructor, there is no way to know if that destruction is the final destruction.An example of when you'd want to reach for the
LifecycleResourceis when you're managing external long-lived state that needs a final destruction call, such as with XState, which requires that the "State machine interpreter" is stopped when you are discarding the parent context (such as a component).An example
Using this Resource is the exact same as
ResourceThere is however a semi-unintuitive technique you could use to continue to use
Resourcefor thefinalteardown: