-
Notifications
You must be signed in to change notification settings - Fork 220
Fix React hook dependency warnings in Cart & Checkout blocks + withAttributes HOC #3314
Fix React hook dependency warnings in Cart & Checkout blocks + withAttributes HOC #3314
Conversation
Size Change: +421 B (0%) Total Size: 1.12 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great PR, unlike my comment here #3285 (review) I feel more confident merging this PR, it makes sense and no change require documentation because the diff is self-documenting
useEffect( () => { | ||
if ( validateOnMount ) { | ||
validateInput(); | ||
if ( isPristine ) { | ||
if ( focusOnMount ) { | ||
inputRef.current.focus(); | ||
} | ||
if ( validateOnMount ) { | ||
validateInput(); | ||
} | ||
setIsPristine( false ); | ||
} | ||
}, [ validateOnMount ] ); | ||
}, [ | ||
focusOnMount, | ||
isPristine, | ||
setIsPristine, | ||
validateOnMount, | ||
validateInput, | ||
] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you merge two useEffects? the general conscious is to separate useEffects unless you have a reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I undid the merge in 14c4ae8.
/** | ||
* @todo Investigate extra POST request on initial cart render. | ||
* | ||
* We have an unfixed bug in our test in which the full cart triggers a POST | ||
* request to `wc/store/cart/update-item` causing the fetch to be called twice. | ||
*/ | ||
expect( fetchMock ).toHaveBeenCalledTimes( 2 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💃 Finally fixed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably also close this
#2813
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes. Thanks for pointing to the issue. I assigned it to me and linked it with this PR.
@@ -3,7 +3,6 @@ | |||
*/ | |||
import { useState, useEffect } from '@wordpress/element'; | |||
import { getAttributes, getTerms } from '@woocommerce/editor-components/utils'; | |||
import { find } from 'lodash'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
assets/js/hocs/with-attributes.js
Outdated
const { selected } = props; | ||
const [ attributes, setAttributes ] = useState( [] ); | ||
const { selected = [] } = props; | ||
const selectedSlug = selected.length ? selected[ 0 ]?.attr_slug : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my experience, when working with an expected data endpoint (like our own REST API), using optional chaining signals a code smell.
Disregarding line 32, do we expect selected
to be an empty array?
Also if you defaulting to an empty array in line 32, length
would evaluate to 0, which would skip directly to null, so no optional chaining required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... good point. I don't think the optional chaining is necessary at all, probably it's a left-over from an in-progress refactor I was doing. Fixed in bca57a5.
Part of #3204.
Related to #3285.
Fixes #2813.
How to test the changes in this Pull Request:
Cart and Checkout blocks
Most of the changes are related to input field validation/displaying errors + the cart item quantity input, so these are the main flows that need to be tested.
withAttributes HOC