Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...