简体   繁体   English

UseEffect,在状态变化时重新运行?

[英]UseEffect, Re-run on state change?

useEffect(() => {
      if (params.usersearch === props.search || "random" === props.search) { 
        console.log('same');
        return;
      }else{
        console.log(props.search);
        console.log('Params is :',params.usersearch);
        let api = `https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=${apiKey}&tags=${params.usersearch}&per_page=24&format=json&nojsoncallback=1`;
        props.handleLoading(true); //changing state
        let fetchedData = axios
          .get(api)
          .then((data) => data.data.photos.photo)
          .catch((err) => console.log(err));
        props.handleData(fetchedData); //changing state
        props.handleSearch(params.usersearch); //changing state
        props.handleLoading(false); //changing state
        const title = document.querySelector("title");
        title.textContent = `Flickr-Photos/${params.usersearch}`;
        }
    },[params.usersearch]);

Hi everyone.大家好。 I have a question , If my useEffect is running and it in between changes state(as I have mentioned when the props.handleLoading function gets triggered ), so is it gonna stop the and re-run the useEffect method or it is gonna complete the code?我有一个问题,如果我的 useEffect 正在运行并且它处于更改状态之间(正如我在 props.handleLoading 函数被触发时提到的那样),那么它会停止并重新运行 useEffect 方法还是会完成代码?

This kind of code execution cannot be implicit stopped.这种代码执行不能被隐式停止。

Answer: The useEffect-callback will be called again, even if the previous is not done答: useEffect-callback 会被再次调用,即使之前没有做完

You could use a debounce or blocking behavior and cancel/ignore the previous action.您可以使用去抖动或阻塞行为并取消/忽略先前的操作。 UseEffect supports a clean-up method. UseEffect 支持一种清理方法。 You could return a function to cancel a timer with a given throttle value (for debounce).您可以返回一个函数来取消具有给定油门值的计时器(用于去抖动)。

As stated by the react docs如反应文档所述

Why did we return a function from our effect?为什么我们从效果中返回一个函数? This is the optional cleanup mechanism for effects.这是效果的可选清理机制。 Every effect may return a function that cleans up after it.每个效果都可能返回一个在它之后进行清理的函数。 This lets us keep the logic for adding and removing subscriptions close to each other.这让我们可以保持添加和删除订阅的逻辑彼此接近。 They're part of the same effect!它们是相同效果的一部分!

When exactly does React clean up an effect? React 究竟何时清理效果? React performs the cleanup when the component unmounts. React 在组件卸载时执行清理。 However, as we learned earlier, effects run for every render and not just once.然而,正如我们之前所了解的,效果会为每次渲染运行,而不仅仅是一次。 This is why React also cleans up effects from the previous render before running the effects next time.这就是为什么 React 还会在下次运行效果之前清理上一次渲染中的效果。 We'll discuss why this helps avoid bugs and how to opt out of this behavior in case it creates performance issues later below.我们将讨论为什么这有助于避免错误以及如何选择退出这种行为,以防它在下面产生性能问题。

Solution解决方案

Stackblitz Example Stackblitz 示例

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

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