useClerk

Access the Clerk object inside your components.

Overview

The useClerk hook accesses the Clerk object. It can be used to retrieve any object in the ClerkJS SDK. Moreover, it allows access to all of the Clerk object's methods, giving you the freedom to build alternatives to any Clerk Component.

If you want to access a ClerkJS object directly, like User or Session, Clerk provides more specific hooks like useUser or useSession. There's probably a more fine-grained Clerk React hook that you can turn to before resorting to the useClerk hook.

There are a couple of alternative methods to retrieve the Clerk object if you cannot use hooks.

Usage

Make sure you've followed the installation guide for Clerk React before running the snippets below.

An example of the useClerk hook in action is shown below. We get access to the Clerk object in order to render a button that opens the sign in form as a modal. Note that if your component is not a descendant of the <ClerkProvider/> component, the useClerk hook will throw an error.

import { useClerk } from "@clerk/clerk-react";
// Your component must be a descendant
// of the <ClerkProvider /> component.
function SignInButton() {
const clerk = useClerk();
return (
<button onClick={() => clerk.openSignIn({})}>
Sign in
</button>
);
}

Alternatives

Make sure you've followed the installation guide for Clerk React before running the snippets below.

There are times where using a hook might be inconvenient. For such cases, there are alternative ways to get access to the Clerk object.

Clerk provides the <WithClerk/> component and the withClerk higher order component directive that will allow your wrapped components to get access to the Clerk object.

withClerk

The withClerk function offers a Higher Order Component (HOC) that you can wrap your own components with, and they will have access to the Clerk object.

The Clerk object will be accessible in the wrapped component under the clerk prop.

import { withClerk } from "@clerk/clerk-react";
class SignInButton extends React.Component {
render() {
return (
<button onClick={() => this.props.clerk.openSignIn({})}>
Sign in
</button>
);
}
}
// Wrap your component with the useClerk HOC.
export const SignInButtonWithClerk = withClerk(SignInButton);

<WithClerk />

f you really want to stretch JSX capabilities and you cannot use the withClerk higher order component, we provide a <WithClerk/> component that accepts a Function as a child. Inside this function, the Clerk object will be accessible.

import { WithClerk } from "@clerk/clerk-react";
class SignInButton extends React.Component {
render() {
return (
<WithClerk>
{(clerk) => (
<button onClick={() => clerk.openSignIn({})}>
Sign in
</button>
)}
</WithClerk>
);
}
}

Typescript support

If you're using Typescript, Clerk provides a WithClerkProp type to make the compiler aware of the clerk prop for your components.

You can use the WithClerkProp type in combination with both the withClerk higher order component and the <WithClerk/> component.

The example below uses the withClerk higher order component.

import { withClerk, WithClerkProp } from "@clerk/clerk-react";
class SignInButton extends React.Component<WithClerkProp<{}>> {
render() {
return (
<button onClick={() => this.props.clerk.openSignUp({})}>
Sign in
</button>
);
}
}
export const SignInButtonWithClerk = withClerk(SignInButton);