简体   繁体   English

React无法根据条件正确渲染div标签

[英]React doesn't properly render div tags based on conditions

When rendering a list of div tags, if the div was already present in the DOM, when running for the first time, then it gets added with less html attributes. 呈现div标签列表时,如果div在DOM中已经存在,则在首次运行时会添加较少的html属性。

here is my render method: 这是我的渲染方法:

render: function() {
    return (
        <div>
             {renderModel(...)}
        </div>
    );
}

And here is the implementation of renderModel(...) 这是renderModel(...)

function renderModel(...) {
    var connections = [];

    // code to come up with the string to compare for

    if (checkString(stringToCheck)) {
        connections.push(renderModel(...));
    } else {
        connections.push(<div onClick={handleClick} value={stringToCheck}>{label}</div>);
    }
    return connections;
}

So the renderModel(...) will recursively render. 因此renderModel(...)将递归渲染。

The checkString(...) function will tell when to directly print the div, or to recursively look for child nodes (of which, the implementation I have skipped in the code). checkString(...)函数将告诉何时直接打印div或递归查找子节点(其中,我已在代码中跳过了实现)。

There is an array which has some strings which are part of the logic of this function. 有一个包含一些字符串的数组,这些字符串是此函数逻辑的一部分。

Some div tags gets printed recursively initially, while others don't. 某些div标签最初会以递归方式打印,而另一些则不会。 Then based on the user action, strings in the array gets changed. 然后根据用户操作,更改数组中的字符串。 So when the rendering runs for a second time - after strings are changed - then the div s which rendered recursively first time needs to be printed without recursion, all goes well, but the div tag gets added without the value or any other properties in the code. 因此,当渲染第二次运行(在更改了字符串之后)之后,需要以递归方式打印第一次以递归方式渲染的div ,而无需递归,一切都进行得很好,但是div标签添加时没有value或其他任何属性。码。

But it is interesting to note that the onClick handle works fine. 但有趣的是, onClick句柄工作正常。 Only the other properties gets skipped. 仅其他属性被跳过。

Any idea how to solve this? 任何想法如何解决这个问题?

正如wintvelt所建议的那样 ,向数组中的项目添加唯一键可以解决此问题。

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

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