Skip to content

Latest commit

 

History

History
44 lines (35 loc) · 1.15 KB

trap.md

File metadata and controls

44 lines (35 loc) · 1.15 KB

trap(type, cb)

Capture errors or detour events

Syntax

  const [ parts, query, { trap } ] = useRouter;
  trap('error', err => {
    setLastError(err);
    parts.splice(0);
    return false;
  })
  const [ parts, query, { trap } ] = useRouter;
  trap('detour', err => {
    setTimeout(() => {
      if (confirm('Are you sure you want to leaving this page?')) {
        err.proceed();
      } else {
        err.prevent();
      }
    }, 50);
    return true;
  })

Parameters

  • type - "error" or "detour"
  • cb - <Function> Callback function

Notes

A detour trap will only receive RouteChangePending objects. It should return true if it intends to make a decision on whether to proceed with the detour. Navigation will be blocked until proceed or prevent is called.

An error trap should return a boolean if it has handled the error. This disables the default error handling mechanism. A return value of true means the error boundary can attempt to reconstruct the component tree (i.e. the error is fixed). false means the error boundary should render nothing until it's given a new component tree.