I have unit tests for my reducers
, as I'm supposed so have. 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?
You can use a logging middleware as described in the Redux Book:
Alternatively, you could change the logging to just append to a global array (your
window._redux
) and you could look in the array when you needed information on a particular state.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$r.store.getState();
or$r.store.dispatch({type:"MY_ACTION"})
into your consoleIf you're using Next JS, you can access the store by:
window.__NEXT_REDUX_STORE__.getState()
You can also dispatch actions, just look at the options you have in
window.__NEXT_REDUX_STORE__
let store = createStore(yourApp); window.store = store;
Now you can access the store from window.store in the console like this:
window.store.dispatch({type:"MY_ACTION"})
In case you would like to see store state for debugging you could do this: