javascript - componentDidUpdate 中的触发事件
问题描述
我有以下代码:
handleValidate = (value: string, e: React.ChangeEvent<HTMLTextAreaElement>) => {
const { onValueChange } = this.props;
const errorMessage = this.validateJsonSchema(value);
if (errorMessage == null) {
// it is sure that JsonInputRef.current exists
this.JsonInputRef.current!.setCustomValidity('');
onValueChange && onValueChange(value, e);
} else {
if (this.JsonInputRef.current) {
this.JsonInputRef.current.setCustomValidity(errorMessage);
}
}
}
componentDidUpdate(prevProps: JsonInputProps) {
if (prevProps.value !== this.props.value) {
this.validateJsonSchema(this.props.value || '');
}
}
触发事件是一种好习惯,componentDidMount
还是更好地实施它setTimeout
?
解决方案
是的,这也是一个做网络请求的好地方,只要你将当前的 props 与以前的 props 进行比较。
您可以立即调用 setState()componentDidUpdate()
但请注意它必须包含在条件中。
注意:如果返回 false
componentDidUpdate()
将不会被调用 。shouldComponentUpdate()
阅读有关componentDidUpdate()的更多信息。
推荐阅读
- visual-studio-code - 我可以为已弃用的组件禁用自动导入和导入建议吗?
- ruby - 从 jekyll-sitemap 插件中获取错误:未定义的方法 `fetch' 为 true:sitemap.xml 中的 TrueClass
- .net - 如果 WPF 类更改,F# 无法加载 FSharp.Core
- php - PHP 警告:array_combine():两个参数应该有相同数量的元素 - CSV 没有正确解析?
- javascript - 如何从 javascript 中读取 mix-manifest.json
- html - CSS/HTML 仅选择一些引导元素
- angular - ngModel 的 ion-radio-group 问题
- dpdk - 每个 NIC 队列一个缓冲池
- excel - Excel VBA自动填充未填充第一行和第一列
- reactjs - 函数未作为道具传递给 React 中的子组件