我在每个列表项中创建了一个包含list和iframe的示例。
http://jsfiddle.net/codez/kpth3szj/
/** @jsx React.DOM */
var TodoList = React.createClass({
createItem: function(item) {
return (
<li>{item.text} <a href="#" onClick={this.props.handleDelete.bind(this, item)}>x</a><br />
<iframe width="560" height="315" src={item.yid} frameborder="0" allowfullscreen></iframe>
</li>
);
},
render: function() {
return <ul>{this.props.items.map(this.createItem)}</ul>;
}
});
var TodoApp = React.createClass({
getInitialState: function() {
return {items: [
{text:'hello',name:'name1',yid:'//www.youtube.com/embed/y3iZACDBapU?autoplay=1'},
{text:'hello2',name:'name2',yid:'//www.youtube.com/embed/dA3nu8ht0jU?autoplay=1'}
], text: ''};
},
handleDelete: function(itemToDelete, e) {
console.info(itemToDelete);
var newItems = _.reject(this.state.items, function(item) {
return item.name == itemToDelete.name
});
this.setState({items: newItems});
},
handleChange: function(e) {
this.setState({text: e.target.value});
},
handleSubmit: function(e) {
e.preventDefault();
var nextItems = this.state.items.concat([this.state.text]);
var nextText = '';
this.setState({items: nextItems, text: nextText});
},
render: function() {
return (
<div>
<h3>TODO</h3>
<TodoList items={this.state.items} handleDelete={this.handleDelete} />
<form onSubmit={this.handleSubmit}>
<input onChange={this.handleChange} value={this.state.text} />
<button>{'Add #' + (this.state.items.length + 1)}</button>
</form>
</div>
);
}
});
React.renderComponent(<TodoApp />, document.body);
如果我从州删除记录,则重新呈现其他记录的iframe。 如果您尝试删除示例中的第一条记录,则第二条视频从头开始。
如何告诉Reactjs不重新渲染状态不变的记录?
答案 0 :(得分:5)
尝试在Todolist createitem函数中添加一个键属性
var TodoList = React.createClass({
createItem: function(item) {
return (
<li key={item.yid}>
{item.text}
<a href="#" onClick={this.props.handleDelete.bind(this, item)}>x</a><br />
<iframe src={item.yid} width="560" height="315" frameborder="0" allowfullscreen></iframe>
</li>
);
},
render: function() {
return <ul>{this.props.items.map(this.createItem)}</ul>;
}
});
http://facebook.github.io/react/docs/multiple-components.html#dynamic-children