简体   繁体   English

如何在reactjs中重建现有的Dom?

[英]How to rebuild a existing Dom in reactjs?

I am currently trying to get reactJS working with the following Setup: 我目前正在尝试使reactJS与以下安装程序一起使用:

I am getting a xml-DOM from the Server. 我从服务器获取xml-DOM。 Its a server side rendered "custom" DOM, which normaly gets consumed by another client (not html, but java) which renders components. 它是服务器端呈现的“自定义” DOM,通常由呈现组件的另一个客户端(不是html,而是java)使用。

<row id="a">
    <label id="aa" width="140" text="Title" />
    <field id="ab" bgpaint="" width="200" text="" enabled="true" />
</row>

I defined some ReactComponents with React.createClass(). 我用React.createClass()定义了一些ReactComponents。 So I have a Label, a Field and a Row (div). 因此,我有一个标签,一个字段和一个行(div)。

Currently I cannot work out a way to render the Components of the xml-DOM with react. 目前,我无法找到一种通过react呈现xml-DOM组件的方法。 I am going trough the xml-DOM and got the snippet from above. 我将通过xml-DOM并从上面获取摘录。 If I find a "row" shell I call React.createElement(Row, {}, ?)? 如果找到“行”外壳,我会调用React.createElement(Row,{},?)? But I i dont know what children will be to fill the '?' 但是我不知道什么孩子会填补“?” before I go deeper into the xml-DOM. 在我更深入地研究xml-DOM之前。 If I walk through the xml-DOM until I got a "endNode" with no children, how can i create the react elements from bottom to top? 如果我遍历xml-DOM直到得到一个没有子节点的“ endNode”,我该如何从下到上创建react元素? Do I have to create a super String and somehow call it as javascript. 我是否必须创建一个超级String并以某种方式将其称为javascript。

Is there a way to create a Row, and afterwads add the children? 有没有办法创建行,然后添加子项? Do I have to use the components props? 我必须使用组件道具吗? Something like: 就像是:

//<row> found
var newRow = React.createElement(Row, {}, ?);
...
//found the label in the row
var newLabel = React.createElement(Label, {}, "Title");
newRow.add(newLabel);

I can hardly come to a solution - is it possible to do with react? 我几乎无法解决-可以做出反应吗? Or am I trying to do something which react is not made for? 还是我正在尝试做一些没有反应的事情? Probably I do not understand the child concept totally. 可能我不太了解孩子的概念。 Or maybe its not really a react-Problem itself... 也许它本身并不是一个反应问题...

Any hints for me? 对我有什么提示吗?

greetz 格尔茨

Create the children array before you call createElement. 在调用createElement之前,请创建子数组。 For example ( jsfiddle ): 例如( jsfiddle ):

function pascalize(str) {
    return str.split("-").map(function(name) {
        return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
    }).join("");
}

function attrsToProps(attrs) {
    var obj = {};
    for (var i = 0; i < attrs.length; ++i) {
        obj[attrs[i].nodeName] = attrs[i].value;
    }
    return obj;
}

function toReact(node, components) {
    if (node.nodeType === 1) {
        var children = [].map.call(node.childNodes, function(child) {
            return toReact(child, components);
        });
        return React.createElement(components[pascalize(node.nodeName)],
                                    attrsToProps(node.attributes),
                                    children);
    } else if (node.nodeType === 3) {
        return (node.nodeValue + "").trim();
    }
    return "";
}

function xmlToReactComponents(xmlDom, components) {
    if (xmlDom && xmlDom.documentElement) {
        return React.createElement("div", [], [].map.call(xmlDom.documentElement.childNodes, function(child) {
            return toReact(child, components);
        }));
    }
}

The usage in jsfiddle is: jsfiddle中的用法是:

var xml = document.querySelector("#xml").innerHTML;
var parser = new DOMParser();
var dom = parser.parseFromString(xml, "text/xml");

var defaultClass = {
    render: function() {
        return React.createElement("div", {}, "id: " + this.props.id, this.props.children);
    }  
};

var components = {
    Row: React.createClass(defaultClass),
    Label: React.createClass(defaultClass),
    Field: React.createClass(defaultClass)
}
var result = xmlToReactComponents(dom, components);
React.render(result, document.body);

You need to pass all the components the xml could create in the object otherwise whole thing fails 您需要传递xml可以在对象中创建的所有组件,否则整个事情都会失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM