javascript - React componentDidMount 不会触发
问题描述
我有一个反应组件,例如带有 componentDidMount 事件的函数。但它根本没有被触发。
function ItemsList() {
return (
<div>This is Items List component</div>
)
function componentDidMount( )
{
alert('aaaaaaaaaaaaaaaaaaaaaaaaaaaa');
}
}
有人可以告诉我这种模式有什么问题吗?
解决方案
Functional components do not have lifecycle methods like componentDidMount
.
You can imitate the lifecycle behaviour of componentDidMount
with the useEffect
hook:
useEffect(() => {
alert('aaaaaaaaaaaaaaaaaaaaaaaaaaaa');
}, [])
推荐阅读
- ffmpeg - 添加图像后如何将视频编解码器保持为原始编解码器?
- python - 创建 scikit-learn 贡献包
- c# - 生成 excel 清除 viewbag/tempdata
- django - 无法在 django 中使用反向 url 功能
- python - 从 WAV 文件中绘制正弦曲线
- vbscript - ADODB 记录集无法从 SQL 数据库中提取“XMLData”(xml 格式数据)
- go - gRPC 可以集成到 Flutter-web 中吗?
- r - 在 R 中,是什么使 NULL 原子化,因此不能存在于向量中?
- reactjs - 使用字符串动态响应 setState
- javascript - javascript迭代器中的递增计数器