【发布时间】:2015-07-08 00:07:50
【问题描述】:
我在 react 中动态渲染组件时遇到此错误
findComponentRoot(..., .0.1.1.0.1.0.5.0.1:4):找不到元素。 这可能意味着 DOM 发生了意外突变(例如,由 浏览器),通常是由于在使用表格时忘记了 a, 嵌套标签,如
我已经检查了标记,看起来不错。有时元素上的错误会发生变化,我的意思是,有时它似乎会影响某个元素,有时它会出现在其他元素上。
这是我的渲染对象:
render: function(){
var _slots = function(id, h, handleClick){
//react way to do this
var daysforSlot = this.props.workingDays.map(function(day, i){
var spanActive = this.state.appointments.map(function(appointment){
var _activeClass = null;
var _s = null;
if(h.hour+day === appointment.hour+appointment.day){
_activeClass = 'active'
}
if(_activeClass){
_s = <span className='active'>{appointment.patient.name}</span>
}
return ( {_s} )
});
return (
<td id={h.hour+day} onClick={handleClick.bind(null, {hour: h.hour, day:i})}>
{spanActive}
</td>
)
}.bind(this));
return(
<div>
<td>{h.text}</td>
<span>{daysforSlot}</span>
</div>
)
}.bind(this);
//creates the working hours for the days and bind the click
//handler to the hour slots
var _schedule = this.props.workingHours.map(function(h, i){
return (
<tr>
<span>{_slots(i, h, this.handleClick)}</span>
</tr>
)
}.bind(this));
return(
<span>
{_schedule}
</span>
)
}
提前感谢任何 cmets。
【问题讨论】:
-
它或多或少意味着它在锡上所说的内容。当您通过 HTML(或 React)创建表格时,即使您不这样做,浏览器也会在 DOM/Virtual DOM 中插入
thead和tbody标签。当你改变你的状态或属性时,这会导致 React 跟踪的父/子映射之间不匹配并导致此错误。请务必输出tbody(以及相关时thead)
标签: javascript reactjs render