Search code examples
javascriptreactjsredux

While debugging, can I have access to the Redux store from the browser console?


I have unit tests for my reducers. However, when I'm debugging in the browser, I want to check if my actions have been called correctly and whether the state has been modified accordingly.

I'm looking for something like:

window._redux.store

... in the browser so I can type that on the console and check how things are going.

How can I achieve that?


Solution

  • How to view redux store on any website, with no code changes

    Update Nov 2019

    The react devtools have changed since my original answer. The new components tab in chrome's devtools still has the data, but you may have to search a little bit more.

    1. open chrome devTools
    2. select react devtool's Components tab
    3. click on the top-most node and look in right-hand column for store to be shown
    4. repeat step 3 down the tree until you find the store (for me it was the 4th level)
    5. Now you can follow the steps below with $r.props.store.getState()

    Example screenshot

    Original Answer

    If you have react developer tools running you can use $r.store.getState(); with no changes to your codebase.

    Note: You have to open the react devtool in your developer tools window first to make this work, otherwise you will get a $r is not defined error

    1. open developer tools
    2. click the React tab
    3. ensure the provider node (or topmost node) is selected
    4. then type $r.store.getState(); or $r.store.dispatch({type:"MY_ACTION"}) into your console