useCurrentUser
The useCurrentUser hook allows you to fetch the profile information of the currently authenticated user. This is useful for building custom account pages or displaying personalized content.
Import
import { useCurrentUser } from '@appmaker-xyz/shopify';
Basic Usage
const { data: user, isLoading } = useCurrentUser({});
if (isLoading) return <LoadingSpinner />;
return (
<View>
<Text>Welcome back, {user?.firstName}!</Text>
<Text>Email: {user?.email}</Text>
</View>
);
Using Metafields
You can fetch specific Shopify customer metafields by passing an array to the hook.
const { data: user } = useCurrentUser({
metafields: [
{ namespace: "custom", key: "loyalty_points" },
{ namespace: "appmaker", key: "membership_type" }
]
});
// Accessing the retrieved metafields
const loyaltyPoints = user?.metafields?.find(
m => m.namespace === "custom" && m.key === "loyalty_points"
)?.value;
Hook API
Parameters
| Parameter | Type | Description |
|---|---|---|
metafields | Array<{ namespace: string, key: string }> | Optional. List of Shopify metafield identifiers to fetch. |
Return Value
| Property | Type | Description |
|---|---|---|
data | Object | The user object containing firstName, email, tags, phone, metafields, etc. |
isLoading | boolean | Indicates if the request is in progress. |
isSuccess | boolean | True if the data was fetched successfully. |
isError | boolean | True if the fetch request failed. |
error | any | Error details if isError is true. |