Skip to content

Commit

Permalink
feat: made data prop reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
JairajJangle committed Dec 9, 2024
1 parent 06b3f7b commit f5f3699
Show file tree
Hide file tree
Showing 5 changed files with 2,068 additions and 1,787 deletions.
10 changes: 9 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"@testing-library/react-native": "^12.6.1",
"@types/color": "^3.0.6",
"@types/jest": "^29.5.3",
"@types/lodash": "^4.17.13",
"@types/react": "~17.0.21",
"@types/react-native": "0.70.0",
"@types/react-native-vector-icons": "^6.4.18",
Expand Down Expand Up @@ -208,6 +209,7 @@
]
},
"dependencies": {
"lodash": "^4.17.21",
"react-native-uuid": "^2.0.3",
"zustand": "^4.5.5"
},
Expand Down
8 changes: 5 additions & 3 deletions src/TreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { useTreeViewStore } from './store/treeView.store';
import usePreviousState from './utils/usePreviousState';
import { useShallow } from "zustand/react/shallow";
import uuid from "react-native-uuid";
import useDeepCompareEffect from "./utils/useDeepCompareEffect";

const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(
(props, ref) => {
Expand Down Expand Up @@ -111,7 +112,9 @@ const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(

const prevSearchText = usePreviousState(searchText);

React.useEffect(() => {
useDeepCompareEffect(() => {
cleanUpTreeViewStore();

updateInitialTreeViewData(data);

if (selectionPropagation)
Expand All @@ -124,8 +127,7 @@ const _TreeView = React.forwardRef<TreeViewRef, TreeViewProps>(

// Expand pre-expanded nodes
expandNodes(storeId, preExpandedIds);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [data]);

function selectNodes(ids: string[]) {
toggleCheckboxes(storeId, ids, true);
Expand Down
29 changes: 29 additions & 0 deletions src/utils/useDeepCompareEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useRef } from "react";
import isEqual from "lodash/isEqual";

/**
* Deep compare effect hook.
* Ensures the effect runs on the first render and whenever dependencies deeply change.
*
* @param effect The effect callback function.
* @param dependencies The dependencies array to compare deeply.
*/
export default function useDeepCompareEffect(
effect: React.EffectCallback,
dependencies: any[]
) {
const previousDependenciesRef = useRef<any[]>();
const isFirstRender = useRef(true);

const hasChanged =
isFirstRender.current || !isEqual(previousDependenciesRef.current, dependencies);

React.useEffect(() => {
if (hasChanged) {
isFirstRender.current = false; // Mark that the first render has passed
previousDependenciesRef.current = dependencies; // Update dependencies reference
return effect();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasChanged]); // Depend only on the change detection flag
};
Loading

0 comments on commit f5f3699

Please sign in to comment.