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

前端 未结 9 1214
难免孤独
难免孤独 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条回答
  •  -上瘾入骨i
    2020-12-12 14:58

    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 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.

提交回复
热议问题