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

前端 未结 9 1192
难免孤独
难免孤独 2020-12-12 14:01

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 h

9条回答
  •  醉梦人生
    2020-12-12 14:40

    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.store.getState()

    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

提交回复
热议问题