admin管理员组文章数量:1134601
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?
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?
Share Improve this question edited Nov 27, 2019 at 2:49 Andre Pena asked Dec 19, 2015 at 17:27 Andre PenaAndre Pena 59.3k53 gold badges208 silver badges257 bronze badges 3 |11 Answers
Reset to default 201How 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.
- open chrome devTools
- select react devtool's
Components
tab - click on the top-most node and look in right-hand column for
store
to be shown - repeat step 3 down the tree until you find the
store
(for me it was the 4th level) - Now you can follow the steps below with
$r.props.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
- open developer tools
- click the React tab
- ensure the provider node (or topmost node) is selected
- then type
$r.store.getState();
or$r.store.dispatch({type:"MY_ACTION"})
into your console
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"})
window.store.getState()
The recommended solution doesn't work for me.
The correct command is:
$r.props.store.getState()
You can use a logging middleware as described in the Redux Book:
/**
* Logs all actions and states after they are dispatched.
*/
const logger = store => next => action => {
console.group(action.type)
console.info('dispatching', action)
let result = next(action)
console.log('next state', store.getState())
console.groupEnd(action.type)
return result
}
let createStoreWithMiddleware = applyMiddleware(logger)(createStore)
let yourApp = combineReducers(reducers)
let store = createStoreWithMiddleware(yourApp)
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'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__
Another answer suggests adding the store to the window, but if you just want access to the store as an object, you can define a getter on the window.
This code needs to be added where you've configured your store - in my app, this is the same file as where <Provider store={store} />
is called.
Now you can type reduxStore
in the console to get an object - and copy(reduxStore)
to copy it to the clipboard.
Object.defineProperty(window, 'reduxStore', {
get() {
return store.getState();
},
});
You can wrap this in an if (process.env.NODE_ENV === 'development')
to disable it in production.
In case you would like to see store state for debugging you could do this:
#import global from 'window-or-global'
const store = createStore(reducer)
const onStateChange = (function (global) {
global._state = this.getState()
}.bind(store, global))
store.subscribe(onStateChange)
onStateChange()
console.info('Application state is available via global _state object.', '_state=', global._state)
Another way I was able to access the redux store in my reducers when simply console.log()
s were not working was as follows:
First import current
like:
import { current } from '@reduxjs/toolkit'
then you can print the values like:
reducers: {
addItem: (state, action) => {
// some stuff
console.log('==>state:');
console.log(current(state));
},
! WARNING! : Since the redux store is in a wrapper, simply console logging it would print: Proxy(Object)...
With react developer tools:
const store = [...__REACT_DEVTOOLS_GLOBAL_HOOK__.reactDevtoolsAgent.internalInstancesById.values()].find(e=>e.elementType.name==="Provider").pendingProps.store
If you want to use the various methods of the redux state and view it as well, you can store it as a temp global variable and access it via the console. In my solution I used Firefox with React Dev Tools installed.
- Open React dev tools, find the topmost component where the
store
is visible. - Right click the
store
variable in the inspector window and click onStore as global variable
. As soon as you do this, you should see some variable name output to the browser's console. Something like$reactTemp0
. - You can now use this variable to access the store and its methods. So if I wanted to execute a get method, I could say
$reactTemp0.getState()
and view the entire state. You can also use other methods, likedispatch
, but there might be better ways of testing those behaviors.
First of all, you need to define the store in the window
object (You can place it in you configureStore
file):
window.store = store;
Then you only need to write in the console the following:
window.store.getState();
Hop this helps.
本文标签:
版权声明:本文标题:javascript - While debugging, can I have access to the Redux store from the browser console? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736765268a1951779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
LogMonitor
to visualize your actions and resulting states. – Michelle Tilley Commented Dec 19, 2015 at 18:07