Zum Inhalt springen

Hooks-Referenz

Rebase stellt React-Hooks bereit, um von jeder Komponente innerhalb des <Rebase> Provider-Baums auf Framework-Funktionen zuzugreifen.

Der Master-Hook – Zugriff auf alles:

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
}

Zugriff auf den Authentifizierungszustand:

import { useAuthController } from "@rebasepro/core";
function UserMenu() {
const auth = useAuthController();
auth.user // Current user (or null)
auth.initialLoading // Loading initial session
auth.signOut() // Log out
auth.getAuthToken() // Get JWT for API calls
auth.extra // Additional user data (roles, etc.)
}

Entitäten programmatisch in einem Seitenfenster öffnen:

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

Methoden:

MethodeBeschreibung
open({ path, entityId, collection })Eine Entität in einem Seitenfenster öffnen
close()Das aktuelle Seitenfenster schließen
replace({ path, entityId, collection })Den Inhalt des aktuellen Seitenfensters ersetzen

Toast-Benachrichtigungen anzeigen:

import { useSnackbarController } from "@rebasepro/core";
function SaveButton() {
const snackbar = useSnackbarController();
const handleSave = async () => {
try {
await saveData();
snackbar.open({ type: "success", message: "Saved successfully!" });
} catch (error) {
snackbar.open({ type: "error", message: "Save failed" });
}
};
}

Zugriff auf Dateispeicheroperationen:

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;
};
}

Hell-/Dunkel-Thema steuern:

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

Öffnen eines Seitendialogs zur Auswahl von Entitäten aus einer Sammlung. Dies ist derselbe Hook, der intern verwendet wird, wenn eine Relationseigenschaft gerendert wird:

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}>Produkt auswählen</button>;
}

Zugriff auf Navigationszustand und aufgelöste Sammlungen:

import { useNavigationController } from "@rebasepro/core";
function MyComponent() {
const navigation = useNavigationController();
navigation.collections // All registered collections
navigation.views // Custom views
navigation.adminViews // Admin-mode views
navigation.getCollection(path) // Get collection for a path
}