Skip to main content

Overview

useUserActions is a low-level hook that provides direct access to the Redux actions and API mutations for the current user. In most cases you should use useUser instead, which wraps useUserActions and provides a higher-level interface. Use useUserActions directly when you need to manually set or clear user state — for example, in a custom authentication flow.

Usage Example

import { useUserActions } from "@replyke/react-js";

function CustomAuth() {
  const { setUser, clearUser } = useUserActions();

  const handleExternalLogin = async (token: string) => {
    const userData = await myAuthService.login(token);
    setUser(userData); // Sync the authenticated user into Replyke state
  };

  const handleLogout = () => {
    clearUser();
  };

  return (
    <>
      <button onClick={() => handleExternalLogin("token")}>Login</button>
      <button onClick={handleLogout}>Logout</button>
    </>
  );
}

Return Values

setUser
(user: AuthUser | null) => void
Manually sets the current user in Redux state. Validates that the user object has an id before setting — partial or empty objects are silently ignored.
clearUser
() => void
Clears the current user from Redux state and resets any error.
updateUser
(params: { projectId: string; userId: string; update: UpdateUserParams; currentUser?: AuthUser }) => Promise<AuthUser>
Calls the update user API and manages loading and error state. Applies optimistic updates for non-file fields. Reverts to currentUser if the request fails.
clearError
() => void
Clears any error stored in the user slice.
Prefer useUser for typical profile management. useUserActions is intended for cases where you need direct control over user state, such as custom authentication integrations.