useOrganization()
The useOrganization()
hook retrieves attributes of the currently active organization.
Parameters
useOrganization()
accepts a single object with the following optional properties:
- Name
invitations
- Type
true | { status?: OrganizationInvitationStatus } & { SharedProperties }
- Description
If set to
true
, all default properties will be used. Otherwise, accepts an object with an optionalstatus
property of typeOrganizationInvitationStatus
and any of the properties described in Shared properties.
- Name
membershipRequests
- Type
true | { status?: OrganizationInvitationStatus } & { SharedProperties }
- Description
If set to
true
, all default properties will be used. Otherwise, accepts an object with an optionalstatus
property of typeOrganizationInvitationStatus
and any of the properties described in Shared properties.
- Name
memberships
- Type
true | { role?: OrganizationCustomRoleKey[] } & { SharedProperties }
- Description
- Name
domains
- Type
true | { enrollmentMode?: OrganizationEnrollmentMode } & { SharedProperties }
- Description
Shared properties
Properties that are shared across the invitations
, membershipRequests
, memberships
, and domains
properties.
- Name
initialPage
- Type
number
- Description
A number that specifies which page to fetch. For example, if
initialPage
is set to 10, it will skip the first 9 pages and fetch the 10th page. Defaults to1
.
- Name
pageSize
- Type
number
- Description
A number that specifies the maximum number of results to return per page. Defaults to
10
.
- Name
keepPreviousData
- Type
boolean
- Description
If
true
, the previous data will be kept in the cache until new data is fetched. Defaults tofalse
.
- Name
infinite
- Type
boolean
- Description
If
true
, newly fetched data will be appended to the existing list rather than replacing it. Useful for implementing infinite scroll functionality. Defaults tofalse
.
OrganizationInvitationStatus
useOrganization()
accepts status
as a property for the invitations
and membershipRequests
parameters.
status
is a string that can be one of the following:
OrganizationCustomRoleKey
useOrganization
accepts role
as a property for the memberships
parameter.
role
is a string that represents the user's role in the organization. Clerk provides the default roles org:admin
and org:member
. However, you can create custom roles as well.
OrganizationEnrollmentMode
useOrganization()
accepts enrollmentMode
as a property for the domains
paramater.
enrollmentMode
is a string that can be one of the following:
- Name
isLoaded
- Type
boolean
- Description
- Name
organization
- Type
Organization
- Description
The currently active organization.
- Name
membership
- Type
OrganizationMembership
- Description
The current organization membership.
- Name
memberships
- Type
PaginatedResources
- Description
Includes a paginated list of the organization's memberships.
- Name
invitations
- Type
PaginatedResources
- Description
Includes a paginated list of the organization's invitations.
- Name
membershipRequests
- Type
PaginatedResources
- Description
Includes a paginated list of the organization's membership requests.
- Name
domains
- Type
PaginatedResources
- Description
Includes a paginated list of the organization's domains.
- Name
data
- Type
any[]
- Description
An array that contains the fetched data.
- Name
count
- Type
number
- Description
The total count of data that exist remotely.
- Name
isLoading
- Type
boolean
- Description
A boolean that is
true
if there is an ongoing request and there is no fetched data.
- Name
isFetching
- Type
boolean
- Description
A boolean that is
true
if there is an ongoing request or a revalidation.
- Name
isError
- Type
boolean
- Description
A boolean that indicates the request failed.
- Name
page
- Type
number
- Description
A number that indicates the current page.
- Name
pageCount
- Type
number
- Description
A number that indicates the total amount of pages. It is calculated based on
count
,initialPage
, andpageSize
.
- Name
fetchPage
- Type
(page: number) => void
- Description
A function that triggers a specific page to be loaded.
- Name
fetchPrevious
- Type
() => void
- Description
A helper function that triggers the previous page to be loaded. This is the same as
fetchPage(page => Math.max(0, page - 1))
.
- Name
fetchNext
- Type
() => void
- Description
A helper function that triggers the next page to be loaded. This is the same as
fetchPage(page => Math.min(pageCount, page + 1))
.
- Name
hasNextPage
- Type
boolean
- Description
A boolean that indicates if there are available pages to be fetched.
- Name
hasPreviousPage
- Type
boolean
- Description
A boolean that indicates if there are available pages to be fetched.
- Name
revalidate
- Type
() => void
- Description
A function that triggers a revalidation of the current page.
- Name
setData
- Type
(data: any[]) => void
- Description
A function that allows you to set the data manually.
How to use the useOrganization()
hook
Expand and paginate attributes
To keep network usage to a minimum, developers are required to opt-in by specifying which resource they need to fetch and paginate through. By default, the memberships
, invitations
, membershipRequests
, and domains
attributes are not populated. You must pass true
or an object with the desired properties to fetch and paginate the data.
Infinite pagination
The following example demonstrates how to use the infinite
property to fetch and append new data to the existing list. The memberships
attribute will be populated with the first page of the organization's memberships. When the "Load more" button is clicked, the fetchNext
helper function will be called to append the next page of memberships to the list.
Simple pagination
The following example demonstrates how to use the fetchPrevious
and fetchNext
helper functions to paginate through the data. The memberships
attribute will be populated with the first page of the organization's memberships. When the "Previous page" or "Next page" button is clicked, the fetchPrevious
or fetchNext
helper function will be called to fetch the previous or next page of memberships.
Notice the difference between this example's pagination and the infinite pagination example above.
To see the different organization features integrated into one application, take a look at our organizations demo repository.
Feedback
Last updated on