React Reference
useTask
useTask
is a hook that manages a single task on the Golem Network. It returns an object with the following properties:
Name | Description |
---|---|
run | The worker function that runs the task. |
isRunning | Boolean indicating whether the task is running. |
error | The error which caused the task to fail. |
result | The result of the task. |
Parameters
This hook takes the TaskExecutor instance, initialized by the useExecutor hook.
Name | Description |
---|---|
executor (required) | The TaskExecutor instance. |
Example
function MyComponent({ executor }) {
const { isRunning, error, result, run } = useTask(executor)
const onClick = () =>
run(async (exe) => {
return (await exe.run('echo', ['Hello world!'])).stdout
})
return (
<div>
<button onClick={onClick} disabled={isRunning}>
Run task
</button>
{isRunning && <div>Task is running...</div>}
{error && <div>Task failed due to {error.toString()}</div>}
{result && <div>Task result: {result}</div>}
</div>
)
}
Was this helpful?