useDataSource
Use this hook to access the data source being used in your FireCMS application.
This controller allows you to fetch and save data from your database (such as Firestore or MongoDB) using the abstraction of collections and entities created by FireCMS.
Available Methods
Section titled “Available Methods”fetchCollection: Fetch data from a collectionlistenCollection: Listen to entities in a given path with real-time updatesfetchEntity: Retrieve an entity given a path and an idlistenEntity: Get real-time updates on one entitysaveEntity: Save an entity to the specified pathdeleteEntity: Delete an entitycheckUniqueField: Check if the given property value is unique in the collectiongenerateEntityId: Generate a new ID for an entity (optional, implementation dependent)
Example
Section titled “Example”import React, { useEffect, useState } from "react";import { useDataSource, Entity } from "@firecms/core";
type Product = { name: string; price: number;};
export function ProductLoader() { const dataSource = useDataSource(); const [products, setProducts] = useState<Entity<Product>[]>([]);
useEffect(() => { dataSource.fetchCollection<Product>({ path: "products", limit: 10 }).then(setProducts); }, [dataSource]);
return ( <div> {products.map(product => ( <div key={product.id}>{product.values.name}</div> ))} </div> );}