dan 2 years ago
committed by GitHub
parent
commit
1ae8be5bbd
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      beta/src/content/apis/react/useCallback.md

6
beta/src/content/apis/react/useCallback.md

@ -807,7 +807,7 @@ Make sure you've specified the dependency array as a second argument!
If you forget the dependency array, `useCallback` will return a new function every time: If you forget the dependency array, `useCallback` will return a new function every time:
```js {7} ```js {7}
function ProductPage({ product, referrerId }) { function ProductPage({ productId, referrer }) {
const handleSubmit = useCallback((orderDetails) => { const handleSubmit = useCallback((orderDetails) => {
post('/product/' + productId + '/buy', { post('/product/' + productId + '/buy', {
referrer, referrer,
@ -820,7 +820,7 @@ function ProductPage({ product, referrerId }) {
This is the corrected version passing the dependency array as a second argument: This is the corrected version passing the dependency array as a second argument:
```js {7} ```js {7}
function ProductPage({ product, referrerId }) { function ProductPage({ productId, referrer }) {
const handleSubmit = useCallback((orderDetails) => { const handleSubmit = useCallback((orderDetails) => {
post('/product/' + productId + '/buy', { post('/product/' + productId + '/buy', {
referrer, referrer,
@ -837,7 +837,7 @@ If this doesn't help, then the problem is that at least one of your dependencies
// .. // ..
}, [productId, referrer]); }, [productId, referrer]);
console.log([product, referrerId]); console.log([productId, referrer]);
``` ```
You can then right-click on the arrays from different re-renders in the console and select "Store as a global variable" for both of them. Assuming the first one got saved as `temp1` and the second one got saved as `temp2`, you can then use the browser console to check whether each dependency in both arrays is the same: You can then right-click on the arrays from different re-renders in the console and select "Store as a global variable" for both of them. Assuming the first one got saved as `temp1` and the second one got saved as `temp2`, you can then use the browser console to check whether each dependency in both arrays is the same:

Loading…
Cancel
Save