source: node_modules/recharts/es6/state/hooks.js

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

Added visualizations

  • Property mode set to 100644
File size: 1.7 KB
Line 
1import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector';
2import { useContext, useMemo } from 'react';
3import { RechartsReduxContext } from './RechartsReduxContext';
4var noopDispatch = a => a;
5export var useAppDispatch = () => {
6 var context = useContext(RechartsReduxContext);
7 if (context) {
8 return context.store.dispatch;
9 }
10 return noopDispatch;
11};
12var noop = () => {};
13var addNestedSubNoop = () => noop;
14var refEquality = (a, b) => a === b;
15
16/**
17 * This is a recharts variant of `useSelector` from 'react-redux' package.
18 *
19 * The difference is that react-redux version will throw an Error when used outside of Redux context.
20 *
21 * This, recharts version, will return undefined instead.
22 *
23 * This is because we want to allow using our components outside the Chart wrapper,
24 * and have people provide all props explicitly.
25 *
26 * If however they use the component inside a chart wrapper then those props become optional,
27 * and we read them from Redux state instead.
28 *
29 * @param selector for pulling things out of Redux store; will not be called if the store is not accessible
30 * @return whatever the selector returned; or undefined when outside of Redux store
31 */
32export function useAppSelector(selector) {
33 var context = useContext(RechartsReduxContext);
34 var outOfContextSelector = useMemo(() => {
35 if (!context) {
36 return noop;
37 }
38 return state => {
39 if (state == null) {
40 return undefined;
41 }
42 return selector(state);
43 };
44 }, [context, selector]);
45 return useSyncExternalStoreWithSelector(context ? context.subscription.addNestedSub : addNestedSubNoop, context ? context.store.getState : noop, context ? context.store.getState : noop, outOfContextSelector, refEquality);
46}
Note: See TracBrowser for help on using the repository browser.