简体   繁体   English

反应:useState - if 语句中的值不正确

[英]React: useState - Value is not correct in if statement

I have a problem with useState in React.我对 React 中的 useState 有疑问。 I want to query the current value of a state when a key is pressed, for key navigatgion.我想在按下某个键时查询 state 的当前值,以进行键导航。 However, the console.log already always returns only the value "1", which actually does not correspond to the current state.但是,console.log 已经总是只返回值“1”,实际上与当前的 state 不对应。

In useEffect, however, the state is always output correctly.然而,在 useEffect 中,state 始终是正确的 output。 The problem is not the useEffect.问题不在于useEffect。 It's the function "checkKey".它是 function “checkKey”。 Here, the console.log(currentPage) always returns "1" which can not be the case.在这里,console.log(currentPage) 总是返回“1”,这是不可能的。

  const [currentPage, setCurrentPage] = state(1);
  const [quotes, setQuotes] = state(["fetching ..."]);
  
  const checkKey = (event) => {
    if (event.keyCode == '37') {
        console.log("Key page:" + currentPage); // Always 1???
        if(currentPage > 1) setCurrentPage(currentPage => currentPage - 1);
    } else if (event.keyCode == '39') {
      console.log("Key page:" + currentPage);
        if(currentPage < 6) setCurrentPage(currentPage => currentPage + 1);
    }
  }

With useEffect (changed slightly here with a custom hook, but the functionality is identical, the return value is always correct) the issue does not occur.使用 useEffect (此处使用自定义挂钩稍作更改,但功能相同,返回值始终正确)不会发生此问题。 The navigation is generally working fine, and after each update, the console.log here outputs the correct value.导航一般工作正常,每次更新后,这里的console.log都会输出正确的值。

  /*currentPage */ onChange(() => {
    if (currentPage === 1) {
      navigateTo('/');
    }
    if (currentPage === 2) {
      navigateTo('/resume');
    }
    if (currentPage === 3) {
      navigateTo('/certificates');
    }
    if (currentPage === 4) {
      navigateTo('/project-cdp');
    }
    if (currentPage === 5) {
      navigateTo('/lor-webedia');
    }
    console.log("New page is: " + currentPage);
  }, [currentPage])

FYI: useEffect Custom Hook.仅供参考:useEffect 自定义挂钩。

export const useEffectExceptOnMount = (effect, dependencies) => {
    const mounted = useRef(false);
    useEffect(() => {
      if (mounted.current) {
        const unmount = effect();
        return () => unmount && unmount();
      } else {
        mounted.current = true;
      }
    }, dependencies);
  
    // Reset on unmount for the next mount.
    useEffect(() => {
      return () => mounted.current = false;
    }, []);
  };

Giving the logic to useEffect solves the issue.给 useEffect 提供逻辑解决了这个问题。 But not sure if it's a clean solution.但不确定这是否是一个干净的解决方案。

  /*currentPage */ onChange(() => {

    switch (currentPage) {
      case 0: setCurrentPage(1); // Min
              break;
      case 1: navigateTo('/')
              break;
      case 2: navigateTo('/resume');
              break;
      case 3: navigateTo('/certificates');
              break;
      case 4: navigateTo('/project-cdp');
              break;
      case 5: navigateTo('/lor-webedia');
              break;
      case 6: setCurrentPage(6); // Max
    }
    console.log("New page is: " + currentPage);
  }, [currentPage])

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

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