Salta ai contenuti

Riferimento agli Hook

Rebase fornisce hook React per accedere alle funzionalità del framework da qualsiasi componente all’interno dell’albero del provider <Rebase>.

L’hook principale — accede a tutto:

import { useRebaseContext } from "@rebasepro/core";
function MyComponent() {
const context = useRebaseContext();
context.dataSource // Data operations
context.storageSource // File operations
context.authController // Auth state
context.navigation // Navigation state
context.sideEntityController // Side panel control
context.snackbarController // Toast notifications
}

Accede allo stato di autenticazione:

import { useAuthController } from "@rebasepro/core";
function UserMenu() {
const auth = useAuthController();
auth.user // Utente corrente (o null)
auth.initialLoading // Caricamento della sessione iniziale
auth.signOut() // Disconnettersi
auth.getAuthToken() // Ottieni JWT per le chiamate API
auth.extra // Dati utente aggiuntivi (ruoli, ecc.)
}

Apre entità programmaticamente in un pannello laterale:

import { useSideEntityController } from "@rebasepro/core";
function OpenProductButton({ productId }) {
const sideEntityController = useSideEntityController();
return (
<button onClick={() => {
sideEntityController.open({
path: "products",
entityId: productId,
collection: productsCollection
});
}}>
Apri Prodotto
</button>
);
}

Metodi:

MetodoDescrizione
open({ path, entityId, collection })Apri un’entità in un pannello laterale
close()Chiudi il pannello laterale corrente
replace({ path, entityId, collection })Sostituisci il contenuto del pannello laterale corrente

Mostra notifiche toast:

import { useSnackbarController } from "@rebasepro/core";
function SaveButton() {
const snackbar = useSnackbarController();
const handleSave = async () => {
try {
await saveData();
snackbar.open({ type: "success", message: "Salvato con successo!" });
} catch (error) {
snackbar.open({ type: "error", message: "Salvataggio fallito" });
}
};
}

Accede alle operazioni di storage dei file:

import { useStorageSource } from "@rebasepro/core";
function FileUploader() {
const storage = useStorageSource();
const upload = async (file: File) => {
const result = await storage.uploadFile({
file,
fileName: file.name,
path: "documents"
});
const url = await storage.getDownloadURL(result.path);
return url;
};
}

Controlla il tema chiaro/scuro:

import { useModeController } from "@rebasepro/core";
function ThemeToggle() {
const mode = useModeController();
return (
<button onClick={mode.toggleMode}>
Corrente: {mode.mode} {/* "light" | "dark" */}
</button>
);
}

Apre un dialogo laterale per la selezione di entità da una collezione. Questo è lo stesso hook utilizzato internamente quando viene renderizzata una proprietà di relazione:

import { useEntitySelectionDialog } from "@rebasepro/core";
function SelectProduct() {
const selectionDialog = useEntitySelectionDialog({
path: "products",
collection: productsCollection,
onSingleEntitySelected: (entity) => {
console.log("Selected:", entity);
}
});
return <button onClick={selectionDialog.open}>Seleziona Prodotto</button>;
}

Accede allo stato di navigazione e alle collezioni risolte:

import { useNavigationController } from "@rebasepro/core";
function MyComponent() {
const navigation = useNavigationController();
navigation.collections // Tutte le collezioni registrate
navigation.views // Viste personalizzate
navigation.adminViews // Viste in modalità admin
navigation.getCollection(path) // Ottieni la collezione per un percorso
}