useUser
The useUser hook returns the current user and functions for managing a Shopify customer account. The customer must have an active authentication session (see useAuth) for these methods to work.

Example

1
import { useUser } from '~/hooks/actions'
2
3
const {
4
user,
5
status,
6
createUserAddress,
7
loadUser,
8
modifyUserAddress,
9
modifyUserDefaultAddress,
10
removeUserAddress
11
} = useUser()
Copied!

Returns

The useUser hook returns an object with these properties:
Property
Description
user
An object representing the current user.
status
A string indicating whether the user's profile is currently being fetched.
A function that adds a new address to the Shopify customer's address book.
loadUser
A function that loads information about the current order's associated user.
A function that updates an address in the Shopify customer's address book.
A function that updates which address is marked as the default address in the Shopify customer's address book.
A function that removes an address from the Shopify customer's address book.

Functions

createUserAddress

The createUserAddress function adds a new address to the Shopify customer's address book.

Arguments:

createUserAddress(address, isDefaultAddress)
Argument
Type
Description
address
Object
New address.
address.address1?
String
(Optional) First line of address.
address.address2?
String
(Optional) Second line of address.
address.city?
String
(Optional) City of address.
address.company?
String
(Optional) Company of address.
address.country?
String
(Optional) Country of address.
address.firstName?
String
(Optional) First name of address.
address.lastName?
String
(Optional) Last name of address.
address.phone?
String
(Optional) Phone number of address.
address.province?
String
(Optional) Province or state of address.
address.zip?
String
(Optional) Zip code of address.
isDefaultAddress?
Boolean
(Optional) Boolean indicating whether this address should be the default address for the user. Defaults to false.

Returns:

Promise<void>: A Promise that resolves when the user address has been added.

Example:

1
import { useUser } from '~/hooks/actions'
2
3
const { createUserAddress } = useUser()
4
5
const address = {
6
address1: '',
7
address2: '',
8
city: '',
9
company: '',
10
country: '',
11
firstName: '',
12
lastName: '',
13
phone: '',
14
province: '',
15
zip: ''
16
}
17
const isDefaultAddress = false
18
await createUserAddress(address, isDefaultAddress)
Copied!

loadUser

The loadUser function reloads the current user. This is automatically called on every page load and with every other useUser method.

Arguments:

None.

Returns:

Promise<void>: A Promise that resolves when the user object has been added to Redux.

Example:

1
import { useUser } from '~/hooks/actions'
2
3
const { loadUser } = useUser()
4
await loadUser()
Copied!

modifyUserAddress

The modifyUserAddress function updates an address in the Shopify customer's address book.

Arguments:

modifyUserAddress(addressId, address, isDefaultAddress)
Argument
Type
Description
addressId
String
Address ID.
address
Object
Modified address.
address.address1?
String
(Optional) First line of address.
address.address2?
String
(Optional) Second line of address.
address.city?
String
(Optional) City of address.
address.company?
String
(Optional) Company of address.
address.country?
String
(Optional) Country of address.
address.firstName?
String
(Optional) First name of address.
address.lastName?
String
(Optional) Last name of address.
address.phone?
String
(Optional) Phone number of address.
address.province?
String
(Optional) Province or state of address.
address.zip?
String
(Optional) Zip code of address.
isDefaultAddress?
Boolean
(Optional) Boolean indicating whether this address should be the default address for the user. Defaults to false.

Returns:

Promise<void>: A Promise that resolves when the user address has been modified.

Example:

1
import { useUser } from '~/hooks/actions'
2
3
const { modifyUserAddress } = useUser()
4
5
const addressId = '123'
6
const address = {
7
address1: '',
8
address2: '',
9
city: '',
10
company: '',
11
country: '',
12
firstName: '',
13
lastName: '',
14
phone: '',
15
province: '',
16
zip: ''
17
}
18
const isDefaultAddress = false
19
await modifyUserAddress(addressId, address, isDefaultAddress)
Copied!

modifyUserDefaultAddress

The modifyUserDefaultAddress function updates which address is marked as the default address in the Shopify customer's address book.

Arguments:

modifyUserDefaultAddress(addressId)
Argument
Type
Description
addressId
String
Address ID.

Returns:

Promise<void>: A Promise that resolves when the user address has been modified.

Example:

1
import { useUser } from '~/hooks/actions'
2
3
const { modifyUserDefaultAddress } = useUser()
4
5
const addressId = '123'
6
await modifyUserDefaultAddress(addressId)
Copied!

removeUserAddress

The removeUserAddress function removes an address from the Shopify customer's address book.

Arguments:

removeUserAddress(addressId)
Argument
Type
Description
addressId
String
Address ID.

Returns:

Promise<void>: A Promise that resolves when the user address has been removed.

Example:

1
import { useUser } from '~/hooks/actions'
2
3
const { removeUserAddress } = useUser()
4
5
const addressId = '123'
6
await removeUserAddress(addressId)
Copied!