javascript - React 只重新渲染修改过的 contenteditable 一次
问题描述
重现这有点奇怪,不得不使用超时来破解它,但它确实模拟了我遇到的问题。
https://jsfiddle.net/mwqyub1v/13/
1) React 渲染div
with abc123
。操作这个字符串(添加一个4
什么的)
2) 在第一次超时之后,React 将删除内容——现在在第二个计时器触发之前手动在框中输入一些内容。.
3) 在第二个计时器上,React确实使用as运行该render
方法(注意日志记录),但 div 没有更新(它不会删除您在 abc123 消失后输入的任何文本)。this.props.value
null
我知道玩 contenteditable 的内容会阻止 React 管理该内容(因此我取消了警告)。但是,为什么我在上面的步骤 1 中修改了文本后组件会正确更新?此外,如果第二个计时器设置value
为其他文本,而不是null
,则组件再次正确更新。
我怎样才能让这个组件始终重置 contenteditable 内容,即使值是null
?
解决方案
之所以没有更新第二次超时,是因为虚拟 dom 没有检测到任何差异。这将根据以下内容设置 innerHTML props
:https ://jsfiddle.net/mwqyub1v/35/ 。
class App extends React.Component {
componentDidMount() {
this.node = React.findDOMNode(this);
}
componentDidUpdate() {
this.node.innerHTML = this.props.value;
}
render() {
console.log('value', this.props.value);
return React.createElement('div', {
contentEditable : true,
suppressContentEditableWarning : true
}, this.props.value);
}
}
推荐阅读
- javascript - 根据锚点的 InnerHTML 生成链接以转换时区
- excel - Excel - Visual Basic - 从另一个工作表上的数据中对工作表上的数据进行排序
- javascript - 文本在滚动时出现/消失
- jmeter - Taurus:将数值(不带引号)从 CSV 拉入 JSON
- python - Docker 上的 Redis:连接到 0.0.0.0:6379 时出现错误 111。拒绝连接
- r - 根据R中的日期将行转换为列
- c++ - 在 Manjaro Linux 上包括 GTK for C++ 的问题
- javascript - 阿波罗联盟实体参考工作不正常
- php - 我可以获得修复 Stripe 结帐代码上的 json 错误的帮助吗?
- python - pandas.read_excel() ImportError:无法导入名称'TYPE_BOOL'