javascript - Nextjs 上的 ContentEditable,更新未显示在状态
问题描述
我有一个消息组件。我有一个 ContentEditable 组件https://www.npmjs.com/package/react-contenteditable我在那里使用,因为我需要在这个“textarea”中添加联系人,但我需要在里面实现 html 代码来分隔每个标签,给它们是一种颜色,等等。
问题是我想阻止字符,用户将无法添加字母,只能添加数字、逗号和空格。我为此创建了一个函数以使用“onChange”,它在控制台中向我显示了正确的数据。但是在框架中它仍然显示用户输入的非法字符。正确的数据处于状态,但它不会在 ContentEditable 框架上更新。
const contentEditable = React.createRef();
let state = { html: "0424" };
const handleChange = evt => {
let htmlf = evt.target.value.replace(/\D/g,''); ;
console.log(htmlf);
state = { html: htmlf };
console.log(state);
};
<ContentEditable
innerRef={contentEditable}
html={state.html} // innerHTML of the editable div
disabled={false} // use true to disable editing
onChange={handleChange} // handle innerHTML change
tagName="numero" // Use a custom HTML tag (uses a div by default)
id="contacts"
/>
解决方案
只需以不同的方式声明组件状态。
constructor(props) {
super(props);
this.state = {
html: "0424"
};
}
contentEditable = React.createRef();
handleChange = evt => {
let htmlf = evt.target.value.replace(/\D/g, "");
console.log(htmlf);
this.setState({ html: htmlf })
console.log(this.state);
};
<ContentEditable
innerRef={this.contentEditable}
html={this.state.html} // innerHTML of the editable div
disabled={false} // use true to disable editing
onChange={this.handleChange} // handle innerHTML change
tagName="numero" // Use a custom HTML tag (uses a div by default)
id="contacts"
/>
解决方案
推荐阅读
- r - flexsurvspline 调用中的“'vmmin' 中的初始值不是有限的”错误
- javascript - 从 NodeJS 中的 gRPC 检索嵌套列表(矩阵)
- java - 将 Solr 索引从 6 升级到 8
- python - 调用时通过 Jinja 模板变量问题在 Flask 中排序
- python - 如何使用类似于 MATLAB 语法的 Numpy 创建向量:-5:0.01:5
- python - 给定输入后,我的“组合生成器”不返回任何内容
- c++ - 比较两个字符串的每个字符。新的并正在寻找输入
- php - Modify user information
- spring - org.springframework.data.relational 404 JHiphipster
- java - In spark what is the difference between setting the conf spark.default.parallelism and calling the method rdd.coalesce()?