简体   繁体   English

React Hooks:状态总是设置回初始值

[英]React Hooks: State always setting back to initial value

Switching over to react hooks and using them for the first time. 切换到反应挂钩并首次使用它们。 My state always seems to be set back to the initial value I pass it (0). 我的状态似乎总是被设置回我通过的初始值(0)。 Code is to have a page automatically scroll down and up. 代码是使页面自动上下滚动。 The page is just practice to displaying various file types. 该页面只是显示各种文件类型的实践。 What happens is the scrollDir variable will switch to being set to either 1 or -1 and 0. So the console will display 1,0,1,0,1,0,1,0 etc... How do I get the state to stay during an update? 发生什么情况是scrollDir变量将切换为1或-1和0。所以控制台将显示1,0、1,0、1,0、1,0等...如何获取状态在更新期间留下来?

function App(props) {

  const [scrollDir, setScrollDir] = useState(0);

  function scrollDown() {
    if(document.documentElement.scrollTop < 10)
    {
      setScrollDir(1);
    }
    else if(document.documentElement.scrollTop >= document.documentElement.scrollHeight - window.innerHeight)
    {
      setScrollDir(-1);
    }

    window.scrollBy(0, scrollDir);
  }

  useEffect(() => {
    setInterval(scrollDown, 100);
  });

  return (
    <StackGrid monitorImagesLoaded={true} columnWidth={"33.33%"} >
      <img src={posterPNG} />
      <img src={posterJPG} />
      <img src={posterGIF} />
      <video src={posterMP4} loop autoPlay muted />
      <Document file={posterPDF}>
        <Page pageNumber={1} />
      </Document>
    </StackGrid>
  );
}

useEffect hook takes second argument, an array. useEffect挂钩接受第二个参数,即数组。 If some value in that array changes then useEffect hook will run again 如果该数组中的某些值发生更改,则useEffect挂钩将再次运行

If you leave that array empty useEfect will run only when component mount - (basicaly like ComponentDidMount lifecycle method) 如果将该数组留空,则useEfect仅在组件装入时运行-(基本类似于ComponentDidMount生命周期方法)

And if you omit that array useEffect will run every time component rerenders 而且,如果您省略该数组,则useEffect将在每次组件重新渲染时运行

For example: 例如:

useEffect runs only once, when component mounts: 当组件挂载时,useEffect仅运行一次:

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

useEffect runs everytime when component rerenders: 每次重新渲染组件时,useEffect都会运行:

useEffect(() => {
 //code 
});

useEffect runs only when some of these variables changes: 仅当以下某些变量发生更改时,useEffect才会运行:

let a = 1;
let b = 2;
    useEffect(() => {
     //code 
    },[a,b]);

Also, if you set return statement in useEffect hook, it has to return a function, and that function will always run before useEffect render 另外,如果在useEffect挂钩中设置return语句,它必须返回一个函数,并且该函数将始终在useEffect渲染之前运行

  useEffect(() => {
    // code
    return () => {
      console.log("You will se this before useEffect hook render");
    };
  }, []);

Using setInterval with hooks isn't very intuitive. setInterval与钩子配合使用不是很直观。 See here for an example: https://stackoverflow.com/a/53990887/3984987 . 参见此处的示例: https : //stackoverflow.com/a/53990887/3984987 For a more in-depth explanation you can read this https://overreacted.io/making-setinterval-declarative-with-react-hooks . 有关更深入的说明,您可以阅读此https://overreacted.io/making-setinterval-declarative-with-react-hooks

This useInterval custom hook is pretty easy to use as well https://github.com/donavon/use-interval . 这个useInterval自定义钩子也非常易于使用https://github.com/donavon/use-interval (It's not mine - I ran across this after responding earlier) (这不是我的-我较早回答后就遇到了这个问题)

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

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