为什么 React 文档建议在 componentDidMount 而不是 componentWillMount 中执行 AJAX? [英] Why do the React docs recommend doing AJAX in componentDidMount, not componentWillMount?

查看:33
本文介绍了为什么 React 文档建议在 componentDidMount 而不是 componentWillMount 中执行 AJAX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解为什么 componentDidMount 适用于需要访问 DOM 的任何内容,但 AJAX 请求不一定或通常需要这样做.

I understand why componentDidMount is appropriate for anything that requires DOM access, but an AJAX request doesn’t necessarily or usually need this.

是什么?

推荐答案

componentDidMount 用于副作用.添加事件侦听器、AJAX、改变 DOM 等.

componentDidMount is for side effects. Adding event listeners, AJAX, mutating the DOM, etc.

componentWillMount 很少有用;特别是如果您关心服务器端渲染(添加事件侦听器会导致错误和泄漏,以及许多其他可能出错的东西).

componentWillMount is rarely useful; especially if you care about server side rendering (adding event listeners causes errors and leaks, and lots of other stuff that can go wrong).

有关于从类组件中删除 componentWillMount 的讨论,因为它与构造函数的用途相同.它将保留在 createClass 组件上.

There is talk about removing componentWillMount from class components since it serves the same purpose as the constructor. It will remain on createClass components.

这篇关于为什么 React 文档建议在 componentDidMount 而不是 componentWillMount 中执行 AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆