【问题标题】:useState React Implementations [closed]useState React 实现 [关闭]
【发布时间】:2021-10-13 15:52:21
【问题描述】:

我看到了两个不同的 useState 实现,我想知道它们的区别。

  const [count, setCount] = useState(0);
  ...
  // First: using var from useState
  setCount(count + 1);
  // Second: using prevState in a function
  setCount(count => count + 1);

【问题讨论】:

标签: reactjs react-native use-state


【解决方案1】:
// Will not be re fired until UI is re rendered e.g you have a cart and you wanna increment the number of products in the cart. That way you can ensure that in case the user hit the increment button twice by accident the number wont increment twice before it‘s re rendered
 setCount(count + 1); 

// Will update count twice unless a rerendered were completed
setCount(count => count + 1);

【讨论】:

    猜你喜欢
    • 2019-02-23
    • 2022-01-20
    • 2021-09-25
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2023-03-30
    相关资源
    最近更新 更多