| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.useAppDispatch = void 0;
|
|---|
| 7 | exports.useAppSelector = useAppSelector;
|
|---|
| 8 | var _withSelector = require("use-sync-external-store/shim/with-selector");
|
|---|
| 9 | var _react = require("react");
|
|---|
| 10 | var _RechartsReduxContext = require("./RechartsReduxContext");
|
|---|
| 11 | var noopDispatch = a => a;
|
|---|
| 12 | var useAppDispatch = () => {
|
|---|
| 13 | var context = (0, _react.useContext)(_RechartsReduxContext.RechartsReduxContext);
|
|---|
| 14 | if (context) {
|
|---|
| 15 | return context.store.dispatch;
|
|---|
| 16 | }
|
|---|
| 17 | return noopDispatch;
|
|---|
| 18 | };
|
|---|
| 19 | exports.useAppDispatch = useAppDispatch;
|
|---|
| 20 | var noop = () => {};
|
|---|
| 21 | var addNestedSubNoop = () => noop;
|
|---|
| 22 | var 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 | */
|
|---|
| 40 | function 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 | } |
|---|