Skip to content

✨ Innovative and open-source visualization application that transforms various data formats, such as JSON, YAML, XML and CSV into interactive graphs.

fix: ensure onCollapseChange fires correctly after state updates

AykutSaraca46d0d0adb09
diff --git a/packages/jsoncrack-react/CHANGELOG.md b/packages/jsoncrack-react/CHANGELOG.md
index 0a56a0fd..0be968c8 100644
--- a/packages/jsoncrack-react/CHANGELOG.md
+++ b/packages/jsoncrack-react/CHANGELOG.md
@@ -5,6 +5,16 @@ All notable changes to `jsoncrack-react` will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## [1.0.5] - 2026-09-14
+
+### Fixed
+
+- `onCollapseChange` now fires after commit instead of from inside a state
+ updater, so consumers that write to a store in the callback no longer
+ trigger React's "Cannot update a component while rendering a different
+ component" warning. It also fires when collapsed paths are pruned after a
+ JSON edit, so the reported list always matches what is rendered.
+
## [1.0.3] - 2026-04-10
### Fixed
diff --git a/packages/jsoncrack-react/package.json b/packages/jsoncrack-react/package.json
index 33ec8b03..1c9e81d1 100644
--- a/packages/jsoncrack-react/package.json
+++ b/packages/jsoncrack-react/package.json
@@ -1,6 +1,6 @@
{
"name": "jsoncrack-react",
- "version": "1.0.4",
+ "version": "1.0.5",
"description": "Reusable JSON Crack canvas as a React component",
"license": "Apache-2.0",
"author": "Aykut Saraç",
diff --git a/packages/jsoncrack-react/src/JSONCrackComponent.tsx b/packages/jsoncrack-react/src/JSONCrackComponent.tsx
index 718ce023..b78c0255 100644
--- a/packages/jsoncrack-react/src/JSONCrackComponent.tsx
+++ b/packages/jsoncrack-react/src/JSONCrackComponent.tsx
@@ -265,6 +265,17 @@ export const JSONCrack = forwardRef<JSONCrackRef, JSONCrackProps>(
onCollapseChangeRef.current = onCollapseChange;
}, [onCollapseChange]);
+ // Skip the initial mount so only actual changes are reported.
+ const hasReportedCollapseRef = useRef(false);
+ useEffect(() => {
+ if (isControlled) return;
+ if (!hasReportedCollapseRef.current) {
+ hasReportedCollapseRef.current = true;
+ return;
+ }
+ onCollapseChangeRef.current?.(internalCollapsedPaths);
+ }, [internalCollapsedPaths, isControlled]);
+
// Prune internally-held collapsed paths that no longer resolve to an
// object/array in the current data (e.g., after a JSON edit).
useEffect(() => {
@@ -347,11 +358,9 @@ export const JSONCrack = forwardRef<JSONCrackRef, JSONCrackProps>(
controlledOnToggle?.(path);
return;
}
- setInternalCollapsedPaths(prev => {
- const next = prev.includes(key) ? prev.filter(p => p !== key) : [...prev, key];
- onCollapseChangeRef.current?.(next);
- return next;
- });
+ setInternalCollapsedPaths(prev =>
+ prev.includes(key) ? prev.filter(p => p !== key) : [...prev, key]
+ );
},
[isControlled, controlledOnToggle, findCollapseButton]
);
@@ -389,12 +398,10 @@ export const JSONCrack = forwardRef<JSONCrackRef, JSONCrackProps>(
}
}
setInternalCollapsedPaths(next);
- onCollapseChangeRef.current?.(next);
},
expandAll: () => {
if (isControlled) return;
setInternalCollapsedPaths([]);
- onCollapseChangeRef.current?.([]);
},
getCollapsedPaths: () => collapsedPathsRef.current ?? [],
}),