source: node_modules/@reduxjs/toolkit/src/query/react/ApiProvider.tsx

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.1 KB
Line 
1import { configureStore } from '@reduxjs/toolkit'
2import type { Context } from 'react'
3import { useContext, useEffect } from './reactImports'
4import * as React from 'react'
5import type { ReactReduxContextValue } from 'react-redux'
6import { Provider, ReactReduxContext } from './reactReduxImports'
7import { setupListeners } from './rtkqImports'
8import type { Api } from '@reduxjs/toolkit/query'
9
10/**
11 * Can be used as a `Provider` if you **do not already have a Redux store**.
12 *
13 * @example
14 * ```tsx
15 * // codeblock-meta no-transpile title="Basic usage - wrap your App with ApiProvider"
16 * import * as React from 'react';
17 * import { ApiProvider } from '@reduxjs/toolkit/query/react';
18 * import { Pokemon } from './features/Pokemon';
19 *
20 * function App() {
21 * return (
22 * <ApiProvider api={api}>
23 * <Pokemon />
24 * </ApiProvider>
25 * );
26 * }
27 * ```
28 *
29 * @remarks
30 * Using this together with an existing redux store, both will
31 * conflict with each other - please use the traditional redux setup
32 * in that case.
33 */
34export function ApiProvider(props: {
35 children: any
36 api: Api<any, {}, any, any>
37 setupListeners?: Parameters<typeof setupListeners>[1] | false
38 context?: Context<ReactReduxContextValue | null>
39}) {
40 const context = props.context || ReactReduxContext
41 const existingContext = useContext(context)
42 if (existingContext) {
43 throw new Error(
44 'Existing Redux context detected. If you already have a store set up, please use the traditional Redux setup.',
45 )
46 }
47 const [store] = React.useState(() =>
48 configureStore({
49 reducer: {
50 [props.api.reducerPath]: props.api.reducer,
51 },
52 middleware: (gDM) => gDM().concat(props.api.middleware),
53 }),
54 )
55 // Adds the event listeners for online/offline/focus/etc
56 useEffect(
57 (): undefined | (() => void) =>
58 props.setupListeners === false
59 ? undefined
60 : setupListeners(store.dispatch, props.setupListeners),
61 [props.setupListeners, store.dispatch],
62 )
63
64 return (
65 <Provider store={store} context={context}>
66 {props.children}
67 </Provider>
68 )
69}
Note: See TracBrowser for help on using the repository browser.