reactjs - react中从类重构为功能钩子组件
问题描述
只是想将我的应用程序从基于类的组件重新设计为基于功能挂钩的组件,并被这个回调 setState 代码块所吸引,并且无法通过工作挂钩来实现这一点:
this.setState((prevState) => ({
items: [
...prevState.items,
{
itemtitle: '',
description: '',
articlenr: '',
amount: '',
unit: '',
price: '',
taxpercentage: '',
total: ''
}
],
showItemDetail: true
}))
解决方案
您是在问如何将其翻译为钩子吗?如果是这样,您将不得不发布您所在州的样子。
如果你的状态是整个对象,你可以这样做
import react, {useState} from "react";
const [items, setItems] = useState([]);
const [showItemDetail, setShowItemDetail] = useState(false)
setItems(items => [...items,
{
itemtitle: '',
description: '',
articlenr: '',
amount: '',
unit: '',
price: '',
taxpercentage: '',
total: ''
}
]
)
setShowItemDetail(true)
推荐阅读
- jpa - 如何在 glassfish-resource.xml [Pyara] 中添加多个连接池
- calculated-field - (Quicksight)如何在按月分组时对“按日不同计数”的每个值求和
- gnuplot - 如何告诉 gnuplot 为 ytics 显示更多数字?
- javascript - Angular Async 管道在单元测试中没有正确更新
- c# - 如何在 xUnit 中设置 cookie
- javascript - 如何获取类别的值,然后将其显示在另一个文本框中
- python - 有没有更简洁的方法将 kwargs 从类传递给函数
- php - 如何从使用 Symfony 的构建器类中捕获我的控制器中的数据
- python - 如何对齐标签?Python
- javascript - Bitcore-lib 签名交易出错