Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get content of Redux store in console without devtools?

With React Devtools installed, I can get store by:

$r.store.getState()

And how to do it without React Devtools?

like image 245
ku8ar Avatar asked Mar 28 '18 17:03

ku8ar


Video Answer


2 Answers

I was in a situation where I could not assign anything to window and I also did not have the opportunity to use react or redux dev tools.

This is obviously undocumented and brittle but it seemed to work for me on a few different sites that had redux. Outputs access to state (store with minor tweaks) within console.

Assumes you are rendering react to a dom node with id react-root.

const appStates = []
const reactRoot = document.getElementById('react-root')
let base

try {
    base = reactRoot._reactRootContainer._internalRoot.current
} catch (e) {
    console.log('Could not get internal root information from reactRoot element')   
}

while (base) {
    try {
        state = base.pendingProps.store.getState()
        // This also sometimes works...
        // state = base.stateNode.store.getState()
        appStates.push(state)
    } catch (e) {
        // no state
    }
    base = base.child
}

console.log('Application States', appStates)
like image 172
Eamonn Gahan Avatar answered Sep 24 '22 14:09

Eamonn Gahan


I guess the simplest way is to assign it to the global window just right after you created your store:

const myStore = createStore();

if(process.env.NODE_ENV !== 'production') {
  window.store = myStore;
}

and later you can access it in browser console like:

store.getState();
like image 43
Tomasz Mularczyk Avatar answered Sep 26 '22 14:09

Tomasz Mularczyk