usePlans()
The usePlans() hook provides access to the subscription plans available in your application. It returns a paginated list of plans and includes methods for managing them.
Parameters
usePlans() accepts a single optional object with the following properties:
Returns
usePlans() returns an object with the following properties:
- Name
-
data - Type
BillingPlanResource[]- Description
An array that contains the fetched data. For example, for the
membershipsattribute, data will be an array of OrganizationMembership objects.
- Name
-
setData - Type
CacheSetter<undefined | ClerkPaginatedResponse<BillingPlanResource>>- Description
A function that allows you to set the data manually.
import { usePlans } from '@clerk/clerk-react/experimental'
export default function PlansList() {
const { data, isLoading, hasNextPage, fetchNext, hasPreviousPage, fetchPrevious } = usePlans({
for: 'user',
pageSize: 10,
})
if (isLoading) {
return <div>Loading plans...</div>
}
return (
<ul>
{data?.map((plan) => (
<li key={plan.id}>
<h3>{plan.name}</h3>
<p>{plan.description}</p>
<p>Is free plan: {!plan.hasBaseFee ? 'Yes' : 'No'}</p>
<p>
Price per month: {plan.currency} {plan.amountFormatted}
</p>
<p>
Price per year: {plan.currency} {plan.annualAmountFormatted} equivalent to{' '}
{plan.currency} {plan.annualMonthlyAmountFormatted} per month
</p>
<h4>Features:</h4>
<ul>
{plan.features.map((feature) => (
<li key={feature.id}>{feature.name}</li>
))}
</ul>
</li>
))}
{hasNextPage && <button onClick={() => fetchNext()}>Next</button>}
{hasPreviousPage && <button onClick={() => fetchPrevious()}>Previous</button>}
</ul>
)
}Infinite pagination
The following example demonstrates how to implement infinite scrolling with plans.
import { usePlans } from '@clerk/clerk-react/experimental'
export default function InfinitePlansList() {
const { data, isLoading, hasNextPage, fetchNext } = usePlans({
for: 'user',
infinite: true,
pageSize: 2,
})
if (isLoading) {
return <div>Loading plans...</div>
}
return (
<div>
<ul>
{data?.map((plan) => (
<li key={plan.id}>
<h3>{plan.name}</h3>
<p>{plan.description}</p>
<p>Is free plan: {!plan.hasBaseFee ? 'Yes' : 'No'}</p>
<p>
Price per month: {plan.currency} {plan.amountFormatted}
</p>
<p>
Price per year: {plan.currency} {plan.annualAmountFormatted} equivalent to{' '}
{plan.currency} {plan.annualMonthlyAmountFormatted} per month
</p>
<h4>Features:</h4>
<ul>
{plan.features.map((feature) => (
<li key={feature.id}>{feature.name}</li>
))}
</ul>
</li>
))}
</ul>
{hasNextPage && <button onClick={() => fetchNext()}>Load more plans</button>}
</div>
)
}Checkout flow with a new payment method
Prompt users to add a new payment method during checkout
Checkout flow for returning users
Prompt users to select an existing payment method during checkout
Feedback
Last updated on