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.
Props
fallback?: React.ComponentType<unknown> | React.ReactElement | null | undefinedEl 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.
Configure UserProvider
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:
Envuelva todos los componentes que necesitan acceder a App Services en
AppProvider.Dentro de
AppProvider, envuelva todos los componentes a los que desea que tenga acceso un usuario autenticado conUserProvider.En
UserProvider, incluya una propiedadfallbackcon 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> ); };
UserProvider Hooks
useUser()
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.UserUna instancia del usuario de Realm actualmente autenticado.