Skip to content

Instantly share code, notes, and snippets.

@lydonchandra
Created October 17, 2022 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydonchandra/ce20e4b4375df742221351dee0f740a2 to your computer and use it in GitHub Desktop.
Save lydonchandra/ce20e4b4375df742221351dee0f740a2 to your computer and use it in GitHub Desktop.
getReduxStore, react v18
// Traverse a dom element
const stores = new Set();
const traverse = (element) => {
let store =
element?.memoizedState?.element?.props?.store
|| element?.pendingProps?.store
|| element?.stateNode?.store;
if (store) {
stores.add(store);
}
if (element.child) {
traverse(element.child);
}
};
// Find the root element for React
const reactRoot = Array.from(document.querySelectorAll("*[id]")).find((el) => el?._reactRootContainer?._internalRoot?.current);
const internalRoot = reactRoot._reactRootContainer._internalRoot.current;
console.log(internalRoot);
// Traverse the root react element to find all Redux States in the app
traverse(internalRoot);
[...stores].forEach((store) => console.log(store.getState()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment