React Reference

useAllocation

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

NameDescription
allocationThe allocation instance.
isLoadingBoolean indicating whether the allocation is being loaded.
errorThe error which caused the allocation to fail.
createFunction to create a new allocation.
loadFunction to load an existing allocation by id.
releaseFunction to release the allocation.
resetHookFunction to reset the hook state.

Parameters

Optionally you can provide the ID of the allocation to be loaded automatically.

NameDescription
id (optional)ID of the allocation to be loaded automatically. If this is provided, the initial value of isLoading will be true

Example

Load an allocation by ID:

const { allocation, isLoading } = useAllocation('some-allocation-id')
if (isLoading) {
  return <p>Loading...</p>
}
if (allocation) {
  return <div>Allocation loaded: {allocation.id}</div>
}

Create a new allocation:

const { allocation, create, isLoading } = useAllocation()
if (isLoading) {
  return <p>Loading...</p>
}
if (allocation) {
  return <div>Allocation created: {allocation.id}</div>
}
return (
  <button
    onClick={() =>
      create({
        budget: 1,
        expirationSec: 15 * 60,
      })
    }
  >
    Create a new allocation
  </button>
)

Using the allocation with useExecutor

const { allocation } = useAllocation('some-allocation-id')
const { executor, initialize } = useExecutor({
  // ... other options
  payment: {
    allocation,
  },
})

Was this helpful?