在react代码中双击
Double post in react code
我正在尝试在react中制作一个todo应用程序。但它一直在重复发布。我做了一个编辑功能,之后它会不断地重复发布。我以导游为榜样。但是我找不到错误。
<!DOCTYPE html>
<html>
<head>
<title>EASY SHIT</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="../../build/react.js"></script>
<script src="../../build/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
<style>
body{
margin-top:30px;
}
a.delete{
color: red;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="app"></div>
</div>
</div>
</div>
<script type="text/babel">
var App = React.createClass({
getInitialState: function (){
return{
text: '',
isEdit: 0,
todos:[
{
id: 1,
text: 'meeting at work'
},
{
id: 2,
text: 'Walk the dog'
},
{
id: 3,
text: 'Eat candy'
}
]
}
},
render: function(){
return(
<div>
<TodoForm
{...this.state}
changeText={this.handleChangeText}
onTodoUpdate={this.handleTodoUpdate}
onTodoAdd={this.handleTodoAdd} />
<TodoList
{...this.state}
editTodo={this.handleTodoEdit}
deleteTodo={this.handleTodoDelete}/>
</div>
)
},
handleTodoAdd: function(text){
var newTodo = {
id: this.state.todos.length + 1,
text: text
}
this.setState({todos: this.state.todos.concat(newTodo)});
},
handleTodoDelete: function(todo){
var todos = this.state.todos;
for(var i = 0;i <todos.length; i++) {
if(todos[i].id == todo.id){
todos.splice(i, 1);
}
}
this.setState({todos: todos});
},
handleTodoEdit: function(todo){
this.setState({
text: todo.text,
isEdit: todo.id
});
},
handleChangeText: function(text){
this.setState({text: text});
},
handleTodoUpdate: function(todo){
var todos = this.state.todos;
for(var i = 0;i <todos.length; i++) {
if(todos[i].id == todo.id){
todos.splice(i, 1);
}
}
todos.push(todo);
this.setState({todos: todos});
}
});
var TodoForm = React.createClass({
render: function(){
return(
<div>
<form onSubmit={this.onSubmit}>
<div className="form-group">
<label>Todo text</label>
<input type="text" ref="text" value={this.props.text} onChange={this.onChange} className="form-control" />
</div>
</form>
</div>
)
},
onChange: function(e){
this.props.changeText(e.target.value);
},
onSubmit: function(e){
e.preventDefault();
var text = this.refs.text.value.trim();
if(!text){
alert('Please enter something');
return;
}
if(this.props.isEdit){
var updatedTodo = {
id:this.props.isEdit,
text: text
}
this.props.onTodoUpdate(updatedTodo);
} else {
this.props.onTodoAdd(text);
}
this.props.onTodoAdd(text);
this.refs.text.value= '';
}
});
var TodoList = React.createClass({
render: function(){
return(
<ul className="list-group">
{
this.props.todos.map(todo => {
return <li className="list-group-item" todo={todo} key ={todo.id}>
<span onClick={this.editTodo.bind(this, todo)}> {todo.text}</span> <a onClick={this.OnDelete.bind(this, todo)}className="delete" href="#">x</a></li>
})
}
</ul>
)
},
OnDelete: function(todo){
this.props.deleteTodo(todo);
},
editTodo: function(todo){
this.props.editTodo(todo);
}
});
ReactDOM.render(
<App />,
document.getElementById('app')
);
</script>
</body>
</html>
在我看来,您似乎没有跟踪您是在编辑现有项目还是在添加新项目。
如果你正在编辑,那么你需要跟踪哪一个,然后在你的状态下替换或更新它,否则追加到列表中。你只是在添加,所以你的应用程序总是认为正在添加一个新项目,因此它看起来像是双重发布,但实际上它添加了一个全新的项目,恰好有更新的文本。
另一种选择是调用delete,然后再调用edit,这将具有相同的效果。
相关文章:
- 使用类从一个标记中双击事件
- onclick函数需要双击,因为类分配延迟
- 了解双击代码标记
- jquery Onclick函数带有导致双击的回调排序函数
- 防止双击执行两次jQuery post请求
- JavaScript双击事件
- 选择“字符串”并使用SeleniumJava双击
- jQuery UI自动完成需要在iOS上双击
- 使用jquery在html中添加双击操作
- 双击泛光灯单击
- 如何在Firefox中禁用视频的单击和双击控件
- 双击可更改树标签
- svg.js/svg平移缩放-双击交互
- 在WordSmith jquery中,双击后什么决定了链接图标的位置
- 宣传单:双击时不要触发点击事件函数
- d3 中是否有点击和双击事件.js力定向图
- 当单选按钮的标签可单击时双击
- Draw2d.js:通过双击将可编辑标签添加到现有连接
- 在react代码中双击
- 如何在点击后禁用ASPxButton以防止在前端代码侧使用javascript双击