Created
October 17, 2022 06:49
-
-
Save lydonchandra/ce20e4b4375df742221351dee0f740a2 to your computer and use it in GitHub Desktop.
getReduxStore, react v18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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