Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

useForceUpdate.js 830B

123456789101112131415161718192021222324252627
  1. import { useReducer } from 'react';
  2. /**
  3. * Returns a function that triggers a component update. the hook equivalent to
  4. * `this.forceUpdate()` in a class component. In most cases using a state value directly
  5. * is preferable but may be required in some advanced usages of refs for interop or
  6. * when direct DOM manipulation is required.
  7. *
  8. * ```ts
  9. * const forceUpdate = useForceUpdate();
  10. *
  11. * const updateOnClick = useCallback(() => {
  12. * forceUpdate()
  13. * }, [forceUpdate])
  14. *
  15. * return <button type="button" onClick={updateOnClick}>Hi there</button>
  16. * ```
  17. */
  18. export default function useForceUpdate() {
  19. // The toggling state value is designed to defeat React optimizations for skipping
  20. var _useReducer = useReducer(function (state) {
  21. return !state;
  22. }, false),
  23. dispatch = _useReducer[1];
  24. return dispatch;
  25. }