Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Referencia de API

UserProvider (@realm/react)

Type signature
UserProvider(props): null | ReactElement<any, any>

Componentes anidados dentro UserProvider puede acceder al objeto de usuario que inició sesión y utilizar los ganchos UserProvider.

  • fallback?: React.ComponentType<unknown> | React.ReactElement | null | undefined El componente de respaldo que se renderiza si no hay ningún usuario autorizado. Esto puede usarse para renderizar una pantalla de inicio de sesión o, de otra forma, gestionar la autenticación.

Componentes envueltos por AppProvider puede acceder a los ganchos useApp y useAuth. Estos componentes solo se renderizan si AppProvider se conecta correctamente al backend de App Services.

Components wrapped by UserProvider can access authenticated users with the useUser hook. These components only render if your app has an authenticated user.

To configure user authentication:

  1. Envuelva todos los componentes que necesitan acceder a App Services en AppProvider.

  2. Dentro de AppProvider, envuelva todos los componentes a los que desea que tenga acceso un usuario autenticado con UserProvider.

  3. En UserProvider, incluya una propiedad fallback con un componente que inicie sesión a un usuario. La aplicación representa este componente si no hay ningún usuario autenticado.

import React from 'react';
import {AppProvider, UserProvider} from '@realm/react';
// Fallback log in component that's defined in another file.
import {LogIn} from './Login';
export const LoginExample = () => {
return (
<ScrollView>
<AppProvider id={APP_ID}>
{/* If there is no authenticated user, mount the
`fallback` component. When user successfully
authenticates, the app unmounts the `fallback`
component (in this case, the `LogIn` component). */}
<UserProvider fallback={LogIn}>
{/* Components inside UserProvider have access
to the user. These components only mount if
there's an authenticated user. */}
<UserInformation />
</UserProvider>
</AppProvider>
</ScrollView>
);
};
Type signature
useUser<FunctionsFactoryType, CustomDataType, UserProfileDataType>(): Realm.User<FunctionsFactoryType, CustomDataType, UserProfileDataType>

The useUser() hook provides access to the logged-in user. For example, you can use useUser() to log the current user out.

When changes to the user object happen, this hook will rerender its parent component. For example, if you call user.refreshCustomData to get updated custom user data, the useUser() parent component will rerender.

function UserInformation() {
const user = useUser();
const {logOut} = useAuth();
const performLogout = () => {
logOut();
};
// Add UI for logging out...
}

Devuelve

  • Realm.User Una instancia del usuario de Realm actualmente autenticado.

Volver

AppProvider (@realm/react)

En esta página