usePolicies
The usePolicies
hook is designed to validate policies against a given wallet address. It acts as a standalone tool for policy validation, similar to how the <GatedComponent/>
gates content but is used directly within your React components to conditionally render or execute logic based on policy validation results.
Usage
To use the usePolicies
hook, you must have a walletAddress
and one or more policyIds
. The hook will return the validation result, along with other useful status flags and data.
import React from 'react';
import { usePolicies } from '@ethpass/sdk';
const MyComponent = ({ walletAddress }) => {
const { valid, data, isLoading, isError, error } = usePolicies(['policy1', 'policy2'], walletAddress);
if (isLoading) return <div>Loading...</div>;
if (isError) return <div>Error: {error.toString()}</div>;
return (
<div>
{valid ? (
<div>Access granted</div>
) : (
<div>Access denied</div>
)}
{/* Optionally use data for more detailed rendering */}
</div>
);
};
Parameters
usePolicies
accepts the following parameters:
policyIds
(string[]
): An array of policy identifiers. These are the policies that will be validated against the provided wallet address.walletAddress
(string
): The wallet address to be validated against the specified policies. Ensure that this address is correctly formatted and non-empty.
Return Value
The usePolicies
hook returns an object containing the following properties:
Name | Type | Description |
---|---|---|
valid | any | Indicates whether the wallet address satisfies all of the specified policies. The type of this property is based on your validation logic. |
data | any | Contains additional information returned from the policy validation process, typically including detailed results of the policy checks. |
isLoading | boolean | A flag indicating if the policy validation request is currently in progress. This can be used to show a loading indicator in your UI. |
isError | boolean | A boolean flag that indicates if an error occurred during the policy validation process. |
error | unknown | The error object or message, if an error occurred during validation. The type is unknown to accommodate various potential error structures. |
Updated 4 months ago