Skip to Content
BBRICKSv0.7.4
DataOverview

DataProvider

Hands one HttpClient (and the UI language) to every data hook below it.

import { DataProvider } from '@ergasti/bricks';

Examples

One client for the app

DataProvider hands an HttpClient to every data hook below it (base URL, CSRF, onUnauthorized), and its language to their messages; without a provider hooks use a same-origin client. This example gives it a pretend server.

Code
import { DataProvider, createHttpClient, useHttpClient, useResource, useDataLanguage } from '@ergasti/bricks';
import { demoServer } from './sample-data';

const client = createHttpClient({
  baseUrl: '/api',
  fetch: demoServer({ 'GET /api/summary': () => ({ sales: 'EGP 184,300', creators: 304 }) }),
});

function Summary() {
  const language = useDataLanguage();
  const sameClient = useHttpClient() === client;
  const summary = useResource<{ sales: string; creators: number }>('/summary');
  return (
    <p>
      Language {language}; the hook got the provider's client: {String(sameClient)}.{' '}
      {summary.status === 'ready' ? `Sales ${summary.data?.sales}, ${summary.data?.creators} creators.` : 'Loading…'}
    </p>
  );
}

function Provider() {
  return (
    <DataProvider client={client}>
      <Summary />
    </DataProvider>
  );
}

Props

Prop

Type

Accessibility

  • No roles, ARIA attributes or key handling are set in this component's own source; it renders native elements and the components it composes.

Read from the component's source and tests. See Accessibility for the patterns the library follows.

useBricks

Source

src/data/DataProvider.tsx (line 26) on GitHub.

On this page