javascript - ReactJS 类组件使用 contenteditable 元素渲染 array.map
问题描述
我遇到了一个无法调试的有趣问题。
目标
在一个类组件上,在 render 函数内部,遍历一个对象数组,state
使用this.state.items.map((item, index) => {})
并返回一个contentEditable
段落元素。
在每个contentEditable
段落元素上,监听onKeyUp
事件。如果使用的键e.which
是 enter (13) 键,则this.state.items
使用被键控元素的索引添加一个新项目,以便在该索引之后使用splice
.
看到预期的结果?
不,新添加的项目在渲染时被放在循环的末尾。
重现的示例情况和步骤:
P
在第一个元素中键入“test1”- 点击回车(
P
创建并聚焦一个新元素) P
在第二个新创建的元素中键入“test2”- 重新关注第一个
P
元素,通过 shift+tab 或单击 - 点击进入
- 查看观察到的结果:
P
创建并聚焦了一个新元素,但它位于列表的末尾,而不是预期的位置,位于“test1”和“test2”P
元素之间
这是我到目前为止的代码:
class MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
items: [this.paragraphTemplate()]
}
}
render() {
return (
<section>
<div>
{this.state.items.map((item, index) => {
return <p ref={item.ref}
key={index}
contentEditable
suppressContentEditableWarning
onKeyUp={e => this.handleParagraphKeyUp(e, index, item)}></p>
})}
</div>
</section>
)
}
handleParagraphKeyUp = (e, index, item) => {
if (e.which === 13) {
let addition = this.paragraphTemplate()
this.setState(state => {
state.items.splice(index + 1, 0, addition)
return {
blocks: state.items
}
}, () => {
addition.ref.current.focus()
/* clear out the br and div elements that the browser might auto-add on "enter" from the element that was focused when the "enter" key was used */
this.state.items[index].ref.current.innerHTML = this.state.items[index].ref.current.innerHTML.replace(/<br\s*[\/]?>/gi, '').replace(/<[\/]?div>/gi, '')
})
return false
}
}
paragraphTemplate = () => {
return {
ref: React.createRef()
}
}
}
export default MyComponent
这是一个带有上面代码的jsfiddle。
如果您采取上述步骤,您将看到我遇到的问题。
如果您需要任何进一步的信息,请告诉我,提前致谢!
PS如果我可以对代码进行任何改进,请告诉我。我在 React 中工作了很短的时间,并且希望得到任何关于如何使它更好/更清洁的反馈。
更新
添加key={index}
到P
元素中。注意:这并不反映任何答案,它只是为了与 ReactJS 列表呈现保持一致而添加的。
解决方案
要呈现项目列表,React 需要 key 来跟踪元素,请参见:https ://reactjs.org/docs/lists-and-keys.html
这是您更新的小提琴,可以正常工作..
<p ref={item.ref}
key={item.id}
contentEditable
suppressContentEditableWarning
onKeyUp={e => this.handleParagraphKeyUp(e,
推荐阅读
- typescript - 我可以在 Typescript 中将此数组转换为 List 或 Set 吗?
- django - request.user 无法正常工作,无法分配给隐藏模型字段
- sql - INSERT 语句中主键的棘手问题
- javascript - 画布图像的定位问题
- javascript - Nuxt auth 根据自定义请求更新身份验证
- windows - 从 PowerShell 中的 .exe URL-Source 获取文件版本
- typescript - 如何在 Vuex 模块中使用 RxJS
- reactjs - 在 match.params 中看不到路由参数
- unity3d - 如何改进 ECS Renderer System v2 以在 Unity 中实例化 50.000 个胶囊?
- python - 如何在用户放大图像后加载图像,但不会