React - getting a component from a DOM element for debugging

前端 未结 10 1042
广开言路
广开言路 2020-11-28 19:02

For the purposes of debugging in the console, is there any mechanism available in React to use a DOM element instance to get the backing React component?

This questi

10条回答
  •  借酒劲吻你
    2020-11-28 19:54

    i wrote this small hack to enable access any react component from its dom node

    var ReactDOM = require('react-dom');
    (function () {
        var _render = ReactDOM.render;
        ReactDOM.render = function () {
            return arguments[1].react = _render.apply(this, arguments);
        };
    })();
    

    then you can access any component directly using:

    document.getElementById("lol").react
    

    or using JQuery

    $("#lol").get(0).react
    

提交回复
热议问题