Browse Source

Merge pull request #1570 from bvaughn/useDebugValue-formatterFn

Added useDebugValue formatter function
main
Brian Vaughn 6 years ago
committed by GitHub
parent
commit
8b333fd528
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      content/docs/hooks-reference.md

11
content/docs/hooks-reference.md

@ -384,3 +384,14 @@ function useFriendStatus(friendID) {
> Tip
>
> We don't recommend adding debug values to every custom hook. It's most valuable for custom hooks that are part of shared libraries.
#### Defer formatting debug values
In some cases formatting a value for display might be an expensive operation. It's also unnecessary unless a hook is actually inspected.
For this reason `useDebugValue` accepts a formatting function as an optional second parameter. This function is only called if the hooks is inspected. It receives the debug value as a parameter and should return a formatted display value.
For example a custom hook that returned a `Date` value could avoid calling the `toDateString` function unnecessarily by passing the following formatter:
```js
useDebugValue(date, date => date.toDateString());
```

Loading…
Cancel
Save