【发布时间】:2022-08-16 23:29:57
【问题描述】:
我在这里关注这个 React 教程:https://ibaslogic.com/how-to-edit-todos-items-in-react/ 来构建一个简单的 TO DO 应用程序。
我还查看了Why onDoubleClick event is not working in React.js?,但在我的示例中没有需要担心的onclick 事件。
我的onDoubleClick 事件应该调用一个函数handleEditing,但是当我双击一个列表项时没有任何反应。
我不确定为什么它不起作用(网络浏览器似乎没有注册双击事件。
下面是我的例子:
import React from \"react\";
import styles from \"./TodoItem.module.css\";
class TodoItem extends React.Component {
state = {
editing: false,
};
handleEditing = () => {
console.log(\"doubleClick\")
this.setState({
editing: true,
});
};
render() {
const completedStyle = {
fontStyle: \"italic\",
color: \"#595959\",
opacity: 0.4,
textDecoration: \"line-through\",
};
const { completed, id, title } = this.props.todo;
let viewMode = {}
let editMode = {}
if (this.state.editing) {
viewMode.display = \"none\"
} else {
editMode.display = \"none\"
}
return (
<li className={styles.item}>
<div onDoubleClick={this.handleEditing} style={viewMode}>
<input
type=\"checkbox\"
className={styles.checkbox}
checked={completed}
onChange={() => this.props.handleChangeProps(id)}
/>
<button onClick={() => this.props.deleteTodoProps(id)}>Delete</button>
<span style={completed ? completedStyle : null}>{title}</span>
</div>
<input type=\"text\" style={editMode} className={styles.textInput} />
</li>
);
}
}
export default TodoItem;
我不认为这是相关的,但这是我的 css:
.item {
font-size: 1.2rem;
list-style-type: none;
padding: 17px 0px;
border-bottom: 1px solid #eaeaea;
}
.checkbox {
margin-right: 15px;
}
.item button {
font-size: 13px;
background: #f1f3f4;
border: none;
cursor: pointer;
float: right;
outline: none;
border-radius: 100px;
height: 50px;
width: 50px;
margin: -10px 0 0 10px;
}
.textInput {
width: 100%;
padding: 10px;
border: 1px solid #dfdfdf;
}
-
我无法重现该错误。你能把你的代码贴在codesandbox上然后贴在这里吗?
标签: javascript reactjs ondoubleclick