useUser
The useUser hook returns a series of methods for managing a Shopify customer account. The customer must have an active authentication session (see useAuth) for these methods to work.
import { useUser } from '@chordcommerce/gatsby-theme-performance'
const {
user,
status,
createUserAddress,
loadUser,
modifyUserAddress,
modifyUserDefaultAddress,
removeUserAddress
} = useUser()user
Represents the current user.
Example:
const { user } = useUser()status
String indicating the status of the user in Redux. This string can be used to determine if the user has been loaded yet, which happens asynchronously after page load.
Example:
import { useUser, UserStatus } from '@chordcommerce/gatsby-theme-performance'
const { status } = useUser()
if (status === UserStatus.Failed) {
// the user could not be loaded, probably because there is no valid authentication session
}createUserAddress()
Adds a new address to the Shopify customer's address book.
Example:
const address = {
address1: '',
address2: '',
city: '',
company: '',
country: '',
firstName: '',
lastName: '',
phone: '',
province: '',
zip: ''
}
const isDefaultAddress = false
await createUserAddress(address, isDefaultAddress)loadUser()
Reloads the current user. This is automatically called on every page load and with every other useUser method.
Example:
await loadUser()modifyUserAddress()
Updates an address in the Shopify customer's address book.
Example:
const addressId = '123'
const address = {
address1: '',
address2: '',
city: '',
company: '',
country: '',
firstName: '',
lastName: '',
phone: '',
province: '',
zip: ''
}
const isDefaultAddress = false
await modifyUserAddress(addressId, address, isDefaultAddress)modifyUserDefaultAddress()
Updates which address is marked as the default address in the Shopify customer's address book.
Example:
const addressId = '123'
await modifyUserDefaultAddress(addressId)removeUserAddress()
Removes an address from the Shopify customer's address book.
Example:
const addressId = '123'
await removeUserAddress(addressId)