React Reference

useTask

useTask is a hook that manages a single task on the Golem Network. It returns an object with the following properties:

NameDescription
runThe worker function that runs the task.
isRunningBoolean indicating whether the task is running.
errorThe error which caused the task to fail.
resultThe result of the task.

Parameters

This hook takes the TaskExecutor instance, initialized by the useExecutor hook.

NameDescription
executor (required)The TaskExecutor instance.

Example

function MyComponent({ executor }) {
  const { isRunning, error, result, run } = useTask(executor)
  const onClick = () =>
    run(async (ctx) => {
      return (await ctx.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?