<CacheProvider />
Manages state, providing all context needed to use the hooks. Should be placed as high as possible in application tree as any usage of the hooks is only possible for components below the provider in the React tree.
- React Web 16+
- React Web 18+
- React Native
- NextJS
- Anansi
import { CacheProvider } from '@data-client/react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<CacheProvider>
<App />
</CacheProvider>,
document.body,
);
Alternatively integrate state with redux
import { CacheProvider } from '@data-client/react';
import ReactDOM from 'react-dom';
ReactDOM.createRoot(document.body).render(
<CacheProvider>
<App />
</CacheProvider>,
);
import { CacheProvider } from '@data-client/react';
import { AppRegistry } from 'react-native';
const Root = () => (
<CacheProvider>
<App />
</CacheProvider>
);
AppRegistry.registerComponent('MyApp', () => Root);
Alternatively integrate state with redux
npm install --save @data-client/ssr @data-client/redux redux
import { DataClientDocument } from '@data-client/ssr/nextjs';
export default DataClientDocument;
import { AppCacheProvider } from '@data-client/ssr/nextjs';
import type { AppProps } from 'next/app';
export default function App({ Component, pageProps }: AppProps) {
return (
<AppCacheProvider>
<Component {...pageProps} />
</AppCacheProvider>
);
}
Anansi (beta) is a fully composable framework for React development with optional Server Side Rendering.
npx @anansi/cli hatch my-project
Anansi includes Reactive Data Client automatically.
Props
interface ProviderProps {
children: ReactNode;
managers?: Manager[];
initialState?: State<unknown>;
Controller?: typeof Controller;
devButton?:
| 'bottom-right'
| 'bottom-left'
| 'top-right'
| 'top-left'
| null;
}
initialState: State<unknown>
type State<T> = Readonly<{
entities: Readonly<{
[fetchKey: string]: { [pk: string]: T } | undefined;
}>;
results: Readonly<{
[url: string]: unknown | PK[] | PK | undefined;
}>;
meta: Readonly<{
[url: string]: { date: number; error?: Error; expiresAt: number };
}>;
}>;
Instead of starting with an empty cache, you can provide your own initial state. This can be useful for testing, or rehydrating the cache state when using server side rendering.
managers?: Manager[]
Default Production:
[new NetworkManager(), new SubscriptionManager(PollingSubscription)];
Default Development:
[
new DevToolsManager(),
new NetworkManager(),
new SubscriptionManager(PollingSubscription),
];
List of Managers use. This is the main extensibility point of the provider.
Controller: typeof Controller
This allows you to extend Controller to provide additional functionality. This might be useful if you have additional actions you want to dispatch to custom Managers
class MyController extends Controller {
doSomething = () => {
console.log('hi');
};
}
const RealApp = (
<CacheProvider Controller={MyController}>
<App />
</CacheProvider>
);
devButton
In development, a small button will appear that gives easy access to browser devtools if installed. This option configures where it shows up, or if null will disable it altogether.
'bottom-right' | 'bottom-left' | 'top-right'| 'top-left' | null
<CacheProvider devButton={null}>
<App/>
</CacheProvider>
<CacheProvider devButton="top-right">
<App/>
</CacheProvider>
defaultProps
import { defaultState, Controller } from '@data-client/core';
CacheProvider.defaultProps = {
initialState: defaultState as State<unknown>,
Controller,
devButton: 'bottom-right',
};