【发布时间】:2015-04-03 03:09:32
【问题描述】:
我正在尝试在 React 中创建一个表格组件,其中列和行都是动态的并且会随着时间而变化。但是,当数据发生变化时,React 会抛出意外的 DOM 变异不变违规。
这是一个演示问题http://jsfiddle.net/69z2wepo/1797/ 的小提琴。如您所见,初始渲染后数据发生变化,React 无法再跟踪 DOM 的状态。
代码在这里:
var Table = React.createClass({
getDefaultProps: function() {
return {
data: [
{
colA: { value: 'foo' },
},
],
};
},
render: function() {
return (
<table>
<thead>
<tr> {
Object.keys(this.props.data[0]).map(function(key) {
return (<th>{ key }</th>);
}.bind(this))
} </tr>
</thead>
<tbody> {
this.props.data.map(function(item) {
return (<tr> { Object.keys(item).map(function(key) {
return (
<td>{ item[key] }</td>
);
}.bind(this)) } </tr>);
}.bind(this))
} </tbody>
</table>
);
}
});
var Main = React.createClass({
componentWillMount: function() {
setTimeout(function() {
var data = [
{
colA: {
value: 'foobar',
},
},
];
this.setState({
data: data,
});
}.bind(this), 3000);
},
getInitialState: function() {
var data = [
{
colA: {
value: 'foo',
},
colB: {
value: 'bar',
}
},
{
colA: {
value: 'foo',
},
colB: {
value: 'bar',
}
}
];
return {
data: data,
};
},
render: function() {
return (<Table data={ this.state.data } />);
},
});
React.render(<Main />, document.body);
console.log(React.renderToString(<Table/>));
我尝试了各种添加关键属性来跟踪各种元素的方法,但似乎没有任何方法可以解决这个问题。
使用 renderToString 渲染表格组件表明 React 在表格的各个级别插入了一堆元素。这是可能的原因吗?在此处查看正在渲染的 DOM:
<table data-reactid=".1" data-react-checksum="1098817301">
<thead data-reactid=".1.0">
<tr data-reactid=".1.0.0">
<span data-reactid=".1.0.0.0"> </span>
<th data-reactid=".1.0.0.1:0">colA</th>
<span data-reactid=".1.0.0.2"> </span>
</tr>
</thead>
<tbody data-reactid=".1.1">
<span data-reactid=".1.1.0"> </span>
<tr data-reactid=".1.1.1:0">
<span data-reactid=".1.1.1:0.0"> </span>
<td data-reactid=".1.1.1:0.1:0"><span data-reactid=".1.1.1:0.1:0.$value:0">foo</span></td>
<span data-reactid=".1.1.1:0.2"> </span>
</tr>
<span data-reactid=".1.1.2"> </span>
</tbody>
</table>
【问题讨论】:
-
我没有在那个小提琴中看到错误?
-
有一个 setTimeout 调用正在运行。您可能需要稍等片刻。
-
...不,没有? jsfiddle.net/reactjs/69z2wepo我认为你的链接有误。
-
你是对的。我很抱歉。正确的链接是jsfiddle.net/69z2wepo/1797
-
您也可以删除
>和{之间的空格。所以<tr>{而不是<tr> {,与结束>和}相同。其原因主要是因为foo <b>bar</b>不应该呈现为 foobar。
标签: javascript reactjs html-table