source: node_modules/recharts/lib/state/hooks.js@ a762898

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

Added visualizations

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