javascript - 将类转换为钩子(在 if 语句中声明)
问题描述
我有一个类中的旧代码,我需要将它转换为钩子。我现在已经成功转换了所有内容,除了state
在 if 语句中声明的一个代码,我转换的其他代码this.setState
在 if 语句中。如何将其转换为钩子?
if (this.props.curentAnalyser) {
this.state = {
...this.state,
name: this.props.curentAnalyser.name,
destination: this.props.curentAnalyser.destination_id,
cameras: this.props.curentAnalyser.cameras,
reset_time: this.props.curentAnalyser.reset_time
};
}
例如应该将它们转换为: setReset_time(props.curentAnalyser.reset_time) 吗?
解决方案
就像您可以在文档中阅读一样
对于状态,您需要使用 useState() 函数
const [count, setCount] = useState(0);
对于生命周期,您需要根据您的状态使用 useEffect() 函数
useEffect(() => {
// do something
}, [count]);
推荐阅读
- angular - 如何使从 github 下载的 Angular 项目工作?
- python - 如何使用 pymongo 在 mongodb 数据库中插入散列密码
- java - Java中传统集合和并发集合中的remove()方法有什么优势?
- python - Python-循环遍历 Pandas Groupby 对象
- java - Dagger 在 Android 应用程序中生成重复的“XXXDialogFragment_MembersInjector”类(程序类型已经存在)
- vba - 在 MS Access 中使用 DLookup 时无效使用 Null
- flutter - 找不到 ubuntu 18.04 'flutter' 命令上的颤振错误
- node.js - Sequelize with Postgresql order by with null values first
- python - How to generate a new instance of a class in recursion
- sql - How to replace only some Guids from a table to another, under conditions