| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.SetCartesianGraphicalItem = void 0;
|
|---|
| 7 | exports.SetPolarGraphicalItem = SetPolarGraphicalItem;
|
|---|
| 8 | var _react = require("react");
|
|---|
| 9 | var _hooks = require("./hooks");
|
|---|
| 10 | var _graphicalItemsSlice = require("./graphicalItemsSlice");
|
|---|
| 11 | var SetCartesianGraphicalItemImpl = props => {
|
|---|
| 12 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 13 | var prevPropsRef = (0, _react.useRef)(null);
|
|---|
| 14 | (0, _react.useLayoutEffect)(() => {
|
|---|
| 15 | if (prevPropsRef.current === null) {
|
|---|
| 16 | dispatch((0, _graphicalItemsSlice.addCartesianGraphicalItem)(props));
|
|---|
| 17 | } else if (prevPropsRef.current !== props) {
|
|---|
| 18 | dispatch((0, _graphicalItemsSlice.replaceCartesianGraphicalItem)({
|
|---|
| 19 | prev: prevPropsRef.current,
|
|---|
| 20 | next: props
|
|---|
| 21 | }));
|
|---|
| 22 | }
|
|---|
| 23 | prevPropsRef.current = props;
|
|---|
| 24 | }, [dispatch, props]);
|
|---|
| 25 | (0, _react.useLayoutEffect)(() => {
|
|---|
| 26 | return () => {
|
|---|
| 27 | if (prevPropsRef.current) {
|
|---|
| 28 | dispatch((0, _graphicalItemsSlice.removeCartesianGraphicalItem)(prevPropsRef.current));
|
|---|
| 29 | /*
|
|---|
| 30 | * Here we have to reset the ref to null because in StrictMode, the effect will run twice,
|
|---|
| 31 | * but it will keep the same ref value from the first render.
|
|---|
| 32 | *
|
|---|
| 33 | * In browser, React will clear the ref after the first effect cleanup,
|
|---|
| 34 | * so that wouldn't be an issue.
|
|---|
| 35 | *
|
|---|
| 36 | * In StrictMode, however, the ref is kept,
|
|---|
| 37 | * and in the hook above the code checks for `prevPropsRef.current === null`
|
|---|
| 38 | * which would be false so it would not dispatch the `addCartesianGraphicalItem` action again.
|
|---|
| 39 | *
|
|---|
| 40 | * https://github.com/recharts/recharts/issues/6022
|
|---|
| 41 | */
|
|---|
| 42 | prevPropsRef.current = null;
|
|---|
| 43 | }
|
|---|
| 44 | };
|
|---|
| 45 | }, [dispatch]);
|
|---|
| 46 | return null;
|
|---|
| 47 | };
|
|---|
| 48 | var SetCartesianGraphicalItem = exports.SetCartesianGraphicalItem = /*#__PURE__*/(0, _react.memo)(SetCartesianGraphicalItemImpl);
|
|---|
| 49 | function SetPolarGraphicalItem(props) {
|
|---|
| 50 | var dispatch = (0, _hooks.useAppDispatch)();
|
|---|
| 51 | (0, _react.useLayoutEffect)(() => {
|
|---|
| 52 | dispatch((0, _graphicalItemsSlice.addPolarGraphicalItem)(props));
|
|---|
| 53 | return () => {
|
|---|
| 54 | dispatch((0, _graphicalItemsSlice.removePolarGraphicalItem)(props));
|
|---|
| 55 | };
|
|---|
| 56 | }, [dispatch, props]);
|
|---|
| 57 | return null;
|
|---|
| 58 | } |
|---|