Overview
TheuseCreateSubscription hook is a custom React hook designed to create a subscription using an API endpoint. It manages the subscription creation process, handles loading and error states, and provides the created subscription details.
Usage
Parameters
authToken(string, required): The authentication token for API requests.
Returns
An object with the following properties:createSubscription(function): A function to create a subscription with the provided payload.loading(boolean): Indicates whether a subscription is currently being created.error(string | null): Contains an error message if an error occurred during the process, otherwise null.subscription(object | null): The created subscription details, or null if no subscription has been created yet.
createSubscription Function
ThecreateSubscription function is used to initiate the subscription creation process.
Parameters
payload(object, required): An object containing the subscription details.planCode(string): The code of the plan to subscribe to.isTrial(boolean): Indicates whether this is a trial subscription.
Returns
This function doesn’t return a value directly, but it updates the hook’s state with the created subscription details.Example
API Details
The hook internally uses the following API endpoint:- URL:
${Config.BILLING_API_URL}/api/v1/subscriptions - Method: POST
- Headers:
- ‘Content-Type’: ‘application/json’
- ‘Authorization’:
Bearer ${authToken}
- Body: JSON string containing
planCodeandisTrial
Error Handling
If an error occurs during the API call, theerror state will be set to the error message. You should handle this in your component to display an appropriate error message to the user.
Performance Considerations
- The hook uses
useStateto manage its internal state, ensuring efficient updates and re-renders. - The API call is made only when the
createSubscriptionfunction is explicitly called, preventing unnecessary requests.
Dependencies
This hook depends on the following:- A
Configobject withBILLING_API_URLdefined