Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the redux store in Safari?

Context

I'm trying to debug a React application but cannot modify the source code to log redux variables. In chrome I'm able to access the redux store via the associated extension but it seems no such equivalent exists for safari.

Question

How can I access the redux store in Safari? Can I do so using the console?

like image 226
Patrick Connors Avatar asked Jan 03 '20 20:01

Patrick Connors


2 Answers

The simplest solution, unfortunately, is to modify the source code to set a global variable to the Redux store. (It may be worth preemptively modifying any applications you can control to do this, just to make Safari easier to debug.)

Without modifying the source code, it should be possible, although it's awkward. The following instructions work for React 16.12.0.

  1. In Safari's Web Inspector (dev tools), go to the Elements tab and find your React root element (the <div id="root"> or similar that you pass to ReactDOM.render).
  2. Click on it. Web Inspector should show a = $0 next to it, indicating that you can now reference that DOM node in the Web Inspector console as $0.
  3. In the Web Inspector's Console tab, type Object.keys($0) and press Enter to see the internal properties that React adds to the DOM node. For my app, I see ["__reactContainere$8yexuoe6iiv", "_reactRootContainer"].
  4. Dump the internal React object to the console by typing $0["__reactContainere$8yexuoe6iiv"] (substituting your internal property name) and pressing Enter.
  5. Inspect the object properties to find the Redux store: on my app, it's under child, under memoizedProps, under store, but this may depend on the specifics of your React component hierarchy and where and how you mount Redux's <Provider>.
  6. Use the store reference you just found to call Redux's getState. For my app, that means typing $0["__reactContainere$8yexuoe6iiv"].child.memoizedProps.store.getState() and pressing Enter.

A simpler one-line alternative to the above:

document.getElementById('root')['_reactRootContainer']._internalRoot.current.child.memoizedProps.store.getState()
like image 148
Josh Kelley Avatar answered Sep 28 '22 11:09

Josh Kelley


In case you are using Nextjs framework, you can achieve this by opening the console in safari. Type window in it. Expand it. Now just check in the window object property. You will find a key something like '__REDUX' or something like that. In my case it was __NEXT_REDUX_STORE__.

Now after you find it just enter the following in your console.:

__NEXT_REDUX_STORE__.getState();

you can now check your current redux state of your application.

like image 23
Anurag Avatar answered Sep 28 '22 09:09

Anurag