简体   繁体   English

在哪里使 API 命中 ComonentDidMount 或在 ComonentDidUpdate [React.JS]

[英]Where to make API hits ComonentDidMount or in ComonentDidUpdate [React.JS]

In an interview, an interviewer asked me that where should you make API-hits in a simple react application?在一次采访中,一位面试官问我,在一个简单的 React 应用程序中,您应该在哪里进行 API 命中? Meaning in which life-cycle method in a Class-Component .含义in which life-cycle method Class-Component in which life-cycle method I knew the answer to be ComponentDidMount - because it is the first life-cycle method where we get complete rendered dom meaning dom is now ready!我知道答案是ComponentDidMount - 因为它是我们获得完整渲染 dom 的第一个生命周期方法,这意味着 dom 现在准备好了! Then he asked, but why NOT in comonentDidUpdate ?然后他问,但为什么comonentDidUpdate

Now, I told him what I had read somewhere, I don't know the exact answer of this -- except ComponentDidMount runs first, so make it there.现在,我告诉他我在某处读到的内容,我不知道这个问题的确切答案——除了 ComponentDidMount 首先运行,所以让它在那里。

Now, can someone tell me if my answer was correct?现在,有人可以告诉我我的答案是否正确吗? Or should we make API-hits in ComponentDidUpdate()?还是我们应该在 ComponentDidUpdate() 中进行 API 命中?

I am confused.我很迷惑。 Kindly, someone explain with reasoning?好心,有人用推理解释一下吗? Thanks in Advance!提前致谢!

It depends on when you want to call the API:这取决于您想要调用 API 的时间:

  1. If an API call is done only once then do componentDidMount如果 API 调用仅完成一次,则执行componentDidMount
  2. If after render based on some state, you want to fetch data again then do it in componentDidUpdate如果基于某个状态渲染后,您想再次获取数据,然后在componentDidUpdate

EDIT: Same scenarios can be handled within functional components using useEffect hook as follows:编辑:可以使用useEffect钩子在功能组件内处理相同的场景,如下所示:

1- Only runs the first time when the components render same as componentDidMount : 1- 仅在组件呈现与componentDidMount同时第一次运行:

useEffect(() => {

   // Run only once when the component renders

}, []);  // Pass empty array as dependencies

2- Run every time when component renders either by props change or by local state change same as componentDidUpdate without comparing previous and current props: 2- 每次在组件通过 props 更改或本地状态更改渲染时运行,与componentDidUpdate相同,而不比较之前和当前的 props:

useEffect(() => {

   // Run every time the component re-renders including the first time

});  // Do NOT pass array dependencies

3- Run only when particular props change, same as componentDidUpdate but with props comparison: 3- 仅在特定道具更改时运行,与componentDidUpdate相同,但有道具比较:

useEffect(() => {

   // Run only when the component prop1 and prop2 changes

}, [prop1, prop2]);  // Pass props as array dependencies

Reference: Using the Effect Hook参考:使用效果挂钩

Lets take an example scenario.让我们举一个例子。

You have a profile page and it has a text box which allows you to update tags.您有一个个人资料页面,它有一个文本框,可让您更新标签。

You do a fetch for the whole profile in the componentDidMount to get all the details and show the content.您在 componentDidMount 中获取整个配置文件以获取所有详细信息并显示内容。

And then componentDidUpdate will have to be used for something like the update on tags, lets say you do a fetch to get tags based on the user input for every 3 letters the user type.然后 componentDidUpdate 将必须用于诸如标签更新之类的事情,假设您根据用户输入的每 3 个字母进行提取以获取标签。 then you use componenDidUpdate to check the state and do the call.然后您使用 componenDidUpdate 检查状态并进行调用。

If you think of the same in functional components we'll have to use useEffect.如果您在功能组件中考虑相同,我们将不得不使用 useEffect。

useEffect(()=>{},[]);

See the array of dependecies, if you pass an empty array it would act similar to componentDidMount.查看依赖数组,如果你传递一个空数组,它的作用类似于 componentDidMount。

And the componentDidUpdate和 componentDidUpdate

useEffect(()=>{},[tagText]);

Here the effect will run only when a change it done to the tagText, but the componenDidUpdate would be different as you will have to compare the previous state and decide whether the text is updated.这里的效果只有在对 tagText 进行更改时才会运行,但是 componenDidUpdate 会有所不同,因为您必须比较之前的状态并决定是否更新文本。

According the Official React Documentation(Link) :根据官方 React 文档(链接)

componentDidMount组件DidMount

componentDidMount() is invoked immediately after a component is mounted (inserted into the tree). 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.如果您需要从远程端点加载数据,这是实例化网络请求的好地方。

This method is a good place to set up any subscriptions.此方法是设置任何订阅的好地方。 If you do that, don't forget to unsubscribe in componentWillUnmount().如果这样做,请不要忘记在 componentWillUnmount() 中取消订阅。

componentDidUpdate()组件DidUpdate()

componentDidUpdate() is invoked immediately after updating occurs. componentDidUpdate() 在更新发生后立即调用。 This method is not called for the initial render.初始渲染不会调用此方法。

Use this as an opportunity to operate on the DOM when the component has been updated.以此为契机,在组件更新后对 DOM 进行操作。 This is also a good place to do network requests as long as you compare the current props to previous props (eg a network request may not be necessary if the props have not changed).这也是进行网络请求的好地方,只要您将当前的 props 与之前的 props 进行比较(例如,如果 props 没有改变,则网络请求可能是不必要的)。

Check out this link for a complete big picture.查看此 链接以获得完整的大图。 This will be handy while learning react.这在学习反应时会很方便。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM