use_resource
Creates a reactive async task with automatic dependency tracking. The future automatically re-runs when any reactive values read inside it change.Signature
Parameters
A function that returns a Future. This function is called immediately and re-runs whenever any reactive values (signals, memos, etc.) read inside it change. The future is automatically cancelled and restarted when dependencies change.
Type Parameters
The output type of the async operation.
The Future type returned by the function. Must resolve to type
T.Returns
A resource handle that provides access to the async task’s state and result. The value is
None while loading and Some(T) when complete.Description
Resources are for async operations that need to react to state changes. Think of them as async memos - they compute a value asynchronously and automatically recompute when dependencies change. Key features:- Automatic reloading: The future re-runs when any reactive dependencies change
- State tracking: Access loading, pending, and ready states
- Cancellation: Previous futures are automatically cancelled when restarting
- Control: Manually pause, resume, restart, or cancel the task
The resource value is
None while the future is running and Some(T) when complete. This makes resources perfect for loading states.Example
Fetch data that reloads when a parameter changes:Resource methods
State control
Cancel the current future and start a new one.
Cancel the future and stop it from running.
Temporarily pause the future.
Resume a paused future.
Clear the value without modifying the task.
State queries
Returns
true if the future is currently running.Returns
true if the future has completed or been stopped.Get the current state:
Pending, Paused, Stopped, or Ready.Get a signal for the resource value.
Suspend rendering until the resource is ready. Use with Suspense boundaries.
Example: Manual control
Related
use_memo- Synchronous memoized computationsuse_coroutine- Long-running async tasks with message passinguse_effect- Side effects without return values