useUpdateEffect

React effect hook that ignores the first invocation (e.g. on mount). The signature is exactly the same as the useEffect hook.

忽略第一次调用的 react 副作用钩子(例如在 mount 上)。 标记与useEffect钩子完全相同。

Usage

  1. import React from 'react'
  2. import {useUpdateEffect} from 'react-use';
  3. const Demo = () => {
  4. const [count, setCount] = React.useState(0);
  5. React.useEffect(() => {
  6. const interval = setInterval(() => {
  7. setCount(count => count + 1)
  8. }, 1000)
  9. return () => {
  10. clearInterval(interval)
  11. }
  12. }, [])
  13. useUpdateEffect(() => {
  14. console.log('count', count) // will only show 1 and beyond
  15. return () => { // *OPTIONAL*
  16. // do something on unmount
  17. }
  18. }) // you can include deps array if necessary
  19. return <div>Count: {count}</div>
  20. };