Docs Menu
Docs Home
/ /
Atlas Device SDKs
/
/

Providers & Hooks

On this page

  • Getting Started with Providers
  • Props
  • Configure your Providers
  • Working with Providers using Hooks
  • useRealm()
  • useObject()
  • useQuery()
  • useApp()
  • useAuth()
  • useEmailPasswordAuth()
  • useUser()
  • Create Context with createRealmContext()
  • Configure More Than One Realm

The @realm/react library offers custom React components that eliminate the boilerplate needed to develop a React app. The components leverage the Provider design pattern to manage user creation, user authentication, and data management.

  • RealmProvider: Work with the configured database.

  • AppProvider: Connect to your App Client for user authentication, only necessary when using Device Sync.

  • UserProvider: Access to the logged-in user object, only necessary when using Device Sync.

The Providers are available to all frameworks used to build with the JavaScript SDK.

Like all React components, you call Providers using html opening and closing tags. Nesting a component within in another component's tags creates a parent-child relationship between them, where child components can access the context created by its parent component.

Components take parameters called Props as input, passed into the opening tag. The props passed into a parent component help create the context inherited by the components it wraps. Each Provider has different props you can use for configuration.

This section details how to configure and expose a single realm using a RealmProvider imported directly from @realm/react. For information about using createRealmContext() to configure a realm or exposing more than one realm, refer to their respective sections below.

To use the Provider's context in your app's components, you can use .

Hooks act as functions used to access states in your app. React offers built-in hooks you can use either on their own or to build custom hooks.

There are two important rules to consider when working with hooks:

  • Hooks can only be used at the top level of a React component.

  • Hooks can only be called in a React component or a custom hook, not in regular JavaScript functions.

The @realm/react library has custom hooks for each Provider you can import and use in any wrapped component.

Type signature
createRealmContext(realmConfig?): RealmContext

Most of the time, you will only use createRealmContext() if you need to configure more than one realm. Otherwise, you should import RealmProvider and hooks directly from @realm/react.

The createRealmContext() method creates a React Context object for a realm with a given Configuration. The Context object contains the following:

  • A Context Provider (referred to as RealmProvider elsewhere) component that wraps around child components and provides them with access to hooks.

  • Various prebuilt Hooks that access the configured realm.

For a detailed guide, refer to Expose More Than One Realm.

Parameters

Returns

  • RealmContext

    An object containing a RealmProvider component, and the useRealm, useQuery and useObject hooks.

When you import RealmProvider from @realm/react, that Provider has a specific context and is associated with a single realm. If you need to configure more than one realm, use createRealmContext() to instantiate a new Provider for each realm.

If you import useRealm(), useQuery(), or useObject() directly from @realm/react, those hooks use the default realm context. To work with more than one realm, you need to destructure a new realm Provider and its associated hooks from the result of createRealmContext(). You should namespace providers to avoid confusion about which Provider and hooks you're working with.

For a detailed guide, refer to Expose More Than One Realm.

For details about createRealmContext(), refer to "Create Context with createRealmContext()" on this page.

← 
 →