How do you inspect a react element's props & state in the console?

前端 未结 5 2092
暗喜
暗喜 2021-01-30 12:24

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 insp

5条回答
  •  Happy的楠姐
    2021-01-30 13:03

    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:

    • Statically store the root level component you got from React.render().
    • Create a global debug helper function that you can use in the console with $0 that accesses your static component.

    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.

提交回复
热议问题