简体   繁体   English

在React中使用componentWillMount或componentDidMount生命周期函数进行异步请求

[英]Use componentWillMount or componentDidMount lifecycle functions for async request in React

I am reading up on react lifecycle and am getting a little confused. 我正在阅读反应生命周期,我感到有点困惑。 Some recommend using componentWillMount to make ajax calls: 有人建议使用componentWillMount进行ajax调用:

https://hashnode.com/post/why-is-it-a-bad-idea-to-call-setstate-immediately-after-componentdidmount-in-react-cim5vz8kn01flek53aqa22mby https://hashnode.com/post/why-is-it-a-bad-idea-to-call-setstate-immediately-after-componentdidmount-in-react-cim5vz8kn01flek53aqa22mby

Calling setState in componentDidMount will trigger another render() call and it can lead to layout thrashing. 在componentDidMount中调用setState将触发另一个render()调用,它可能导致布局颠簸。

and in other places it says not to put ajax calls in the componentWillMount: 而在其他地方,它说不要在componentWillMount中放置ajax调用:

https://medium.com/@baphemot/understanding-reactjs-component-life-cycle-823a640b3e8d https://medium.com/@baphemot/understanding-reactjs-component-life-cycle-823a640b3e8d

...this function might end up being called multiple times before the initial render is called so might result in triggering multiple side-effects. ...在调用初始渲染之前,此函数最终可能会被多次调用,因此可能会导致触发多个副作用。 Due to this fact it is not recommended to use this function for any side-effect causing operations. 由于这一事实,不建议将此功能用于任何引起副作用的操作。

Which is correct? 哪个是对的?

The React docs recommend on using componentDidMount for making network Requests React文档建议使用componentDidMount进行网络请求

componentDidMount() is invoked immediately after a component is mounted. 在装入组件后立即调用componentDidMount() Initialization that requires DOM nodes should go here. 需要DOM节点的初始化应该放在这里。 If you need to load data from a remote endpoint, this is a good place to instantiate the network request. 如果需要从远程端点加载数据,这是实例化网络请求的好地方。

Calling setState() in this method will trigger an extra rendering, but it is guaranteed to flush during the same tick. 在此方法中调用setState()将触发额外的渲染,但保证在同一刻度期间刷新。 This guarantees that even though the render() will be called twice in this case, the user won't see the intermediate state. 这保证了即使在这种情况下将调用render()两次,用户也不会看到中间状态。

As per the the case for componentWillMount : 根据componentWillMount的情况:

EDIT: 编辑:

This lifecycle is deprecated since v16.3.0 of react and is no longer encouraged for usage.However its renamed to UNSAFE_componentWillUpdate and is expected to work till at least v17 of react 因为这个生命周期已被弃用v16.3.0反应的,不再鼓励usage.However其重命名为UNSAFE_componentWillUpdate ,预计工作到的反应至少V17

Before v16.3.0 在v16.3.0之前

An asynchronous call to fetch data will not return before the render happens. 在呈现发生之前,不会返回对获取数据的异步调用。 This means the component will render with empty data at least once. 这意味着组件将使用空数据呈现至少一次。

There is no way to “pause” rendering to wait for data to arrive. 没有办法“暂停”渲染以等待数据到达。 You cannot return a promise from componentWillMount or wrangle in a setTimeout somehow. 你不能返回从一个承诺componentWillMount在一个或争吵setTimeout莫名其妙。 The right way to handle this is to setup the component's initial state so that it's valid for rendering. 处理此问题的正确方法是设置组件的初始状态,以使其对渲染有效。

To Sum it up 把它们加起来

In practice, componentDidMount is the best place to put calls to fetch data, for two reasons: 实际上, componentDidMount是调用获取数据的最佳位置,原因有两个:

  • Using DidMount makes it clear that data won't be loaded until after the initial render. 使用DidMount可以清楚地表明,在初始渲染之后才会加载数据。 This reminds you to set up initial state properly, so you don't end up with undefined state that causes errors. 这会提醒您正确设置初始状态,因此不会导致导致错误的undefined状态。
  • If you ever need to render your app on the server, componentWillMount will actually be called twice – once on the server, and again on the client – which is probably not what you want. 如果您需要在服务器上呈现您的应用程序, componentWillMount实际上将被调用两次 - 一次在服务器上,再一次在客户端上 - 这可能不是您想要的。 Putting the data loading code in componentDidMount will ensure that data is only fetched from the client. 将数据加载代码放在componentDidMount中将确保仅从客户端获取数据。

componentDidMount是按照其文档中的描述进行Ajax调用的推荐生命周期方法

ComponentDidMount is the place. ComponentDidMount就是这个地方。

But if you have time try to look at Redux and make the requests in actions, as your application grow it will help a lot to manage the app state. 但是如果你有时间尝试查看Redux并在操作中发出请求,那么随着应用程序的增长,它将有助于管理应用程序状态。

;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native组件未使用componentWillMount或componentDidMount中的异步redux操作呈现 - React Native component not rendering with async redux action in componentWillMount or componentDidMount 何时使用componentWillMount而不是componentDidMount - when to use componentWillMount instead of componentDidMount componentDidMount 或 componentWillMount 在 React-Native 中不起作用? - componentDidMount or componentWillMount not working in React-Native? 在 React.js 中,我应该在 componentWillMount 还是 componentDidMount 中进行初始网络请求? - In React.js should I make my initial network request in componentWillMount or componentDidMount? 我需要使用componentDidMount或componentWillMount - componentDidMount or componentWillMount which one I need to use 如何通过React componentDidMount()方法使用异步等待? - How to use async await with React componentDidMount() method? 使用Ref实现ComponentDidMount LifeCycle事件 - React ComponentDidMount LifeCycle Event With Ref 如何在 React Hooks 中使用 componentWillMount()? - How to use componentWillMount() in React Hooks? 在 componentDidMount 生命周期的响应上拆分 function -- React - Split function on componentDidMount lifecycle's response -- React componentDidMount生命周期方法中的条件异步动作不断循环 - conditional async actions in componentDidMount lifecycle method keeps going on a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM