React Developer Tools give a lot of power to inspect the React component tree, and look at props, event handlers, etc. However, what I'd really like to do is to be able to inspect those data structures in the browser console.
In chrome I can play with the currently selected DOM element in the console using $0
. Is there a way to extract React component info from $0
, or is it possible to do something similar with the React Dev Tools?
press F12 or Ctrl+Shift+I or right click inspect you can get inspect element window. However in inspect there are three developer tools available to debug. “Console”, “Network” and “React developer tools”.
a React Element. We can check if an object is a function component by checking that it's a function and that it contains the 'return React. createElement' code. To check for a class component we can check for type 'function' .
Access React components and state from the consoleSelect a component in React DevTools and pop open the console (hitting the escape key lets you see both at once). Type in $r , and you'll have access to the instance of that React component from the console.
You can click the eye icon in the Developer Tools toolbar to inspect the DOM element, and also if you use the first icon, the one with the mouse icon (which conveniently sits under the similar regular DevTools icon), you can hover an element in the browser UI to directly select the React component that renders it.
Using React Developer Tools you can use $r
to get a reference to the selected React Component.
The following screenshot shows you that I use React Developer Tools
to select a component (Explorer
) which has a state-object callednodeList
. In the console I can now simply write $r.state.nodeList
to reference this object in the state. Same works with the props (eg.: $r.props.path
)
An answer to your question can be found here in a similar question I asked: React - getting a component from a DOM element for debugging
I'm providing an answer here because I don't have the necessary reputation points in order to mark as duplicate or to comment above.
Basically, this is possible if you are using the development build of react because you can leverage the TestUtils to accomplish your goal.
You need to do only two things:
So the code in the console might look something like:
> getComponent($0).props
The implementation of getComponent can use React.addons.TestUtils.findAllInRenderedTree
to search for match by calling getDOMNode on all the found components and matching against the passed in element.
Open console (Firefox,Chrome) and locate any reactjs rendered DOM element or alternatively execute js script to locate it:
document.getElementById('ROOT')
Then check for element properties in object property viewer for attributes with name beginning like '__reactInternalInstace$....' expand _DebugOwner and see stateNode.
The found stateNode will contain (if it has) 'state' and 'props' attributes which is used heavily in reactjs app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With