Overview
TheuseSendEmail hook is a custom React hook designed to send emails using an API endpoint. It manages the email sending process, handles loading and error states, and provides feedback on the success of the operation.
Usage
Parameters
authToken(string, required): The authentication token for API requests.
Returns
An object with the following properties:sendEmail(function): A function to send an email with the provided data.loading(boolean): Indicates whether an email is currently being sent.error(string | null): Contains an error message if an error occurred during the process, otherwise null.success(boolean): Indicates whether the email was sent successfully.
sendEmail Function
ThesendEmail function is used to initiate the email sending process.
Parameters
data(SendEmailData, required): An object containing the email details.email(string): The recipient’s email address.subject(string): The subject of the email.template(ReactNode): The React component to be rendered as the email content.
Returns
This function doesn’t return a value directly, but it updates the hook’s state with the result of the email sending process.Example
API Details
The hook internally uses the following API endpoint:- URL:
${Config.BILLING_API_URL}/api/v1/emails/send - Method: POST
- Headers:
- ‘Content-Type’: ‘application/json’
- ‘Authorization’:
Bearer ${authToken}
- Body: JSON string containing
to,subject, andtext(rendered HTML content)
Error Handling
If an error occurs during the API call, theerror state will be set to the error message returned by the API or a default 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 states (loading, error, and success). - The API call is made only when the
sendEmailfunction is explicitly called, preventing unnecessary requests. - The hook uses
react-dom/server’srenderToStaticMarkupto convert React components to HTML strings, which is efficient for server-side rendering of email content.
Dependencies
This hook depends on the following:- A
Configobject withBILLING_API_URLdefined react-dom/serverfor rendering React components to HTML strings