javascript - 在功能组件中,我应该在哪里放置 console.log 来测试组件是否重新呈现?
问题描述
如何检查功能组件何时更新和/或重新呈现?
在一个类组件中,我可以使用componentWillUpdate
一个console.log
消息。我将在哪里放置console.log
功能组件以获得相同的控制台输出?
我看到两个地方,组件的函数体,我会把它放在开头。
在 jsx 内部。那些会给我同样的结果吗?
我在问,因为我无法React.memo
上班,我想知道我是否测试了错误的东西。
解决方案
使用 React 16.8 官方文档中的效果挂钩
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
推荐阅读
- go - 结构初始化:pU 文字中的值太少或 pU 文字中未导出字段“c”的隐式分配?
- c - 一旦你已经声明了它,你如何在 C 中编辑一个字符串?
- lsf - 有没有办法在创建工作时在 lsf 中标记工作,以后可以搜索?
- css - CSS:无法建立分页符
- mongodb - 使用过滤器在graphql查询中保留顺序
- arrays - 雪花 JSON 解析在数组(列表)中没有“键”的情况下展平项目
- r - 为什么我构建的 R 函数会产生错误的输出?
- angular - 当我在 tomcat 中部署我的 Angular 应用程序时,它会在 url 中创建两个文件夹名称导致 404 错误
- javascript - 我正在使用 JavaScript 创建一个避免游戏的块
- javascript - 使用 TS 从本地存储中转换检索到的对象