Skip to content

Commit 9fb089e

Browse files
committed
Fix React hook order by moving useState on refresh button
1 parent 878638a commit 9fb089e

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

resources/backend/js/gutenberg.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,10 @@ function convertKitGutenbergRegisterBlock(block) {
367367
const EditBlock = function (props) {
368368
const blockProps = useBlockProps();
369369

370-
console.log(props);
371-
console.log(blockProps);
370+
// Refresh button disabled state on DisplayNoticeWithLink.
371+
// This must be here to avoid React error on hook order change when the user e.g.
372+
// connects their Kit account from within the block itself.
373+
const [buttonDisabled, setButtonDisabled] = useState(false);
372374

373375
// If requesting an example of how this block looks (which is requested
374376
// when the user adds a new block and hovers over this block's icon),
@@ -386,7 +388,12 @@ function convertKitGutenbergRegisterBlock(block) {
386388
// If no access token has been defined in the Plugin, or no resources exist in Kit
387389
// for this block, show a message in the block to tell the user what to do.
388390
if (!block.has_access_token || !block.has_resources) {
389-
return DisplayNoticeWithLink(props, blockProps);
391+
return DisplayNoticeWithLink(
392+
props,
393+
blockProps,
394+
buttonDisabled,
395+
setButtonDisabled
396+
);
390397
}
391398

392399
// Build Inspector Control Panels, which will appear in the Sidebar when editing the Block.
@@ -525,14 +532,18 @@ function convertKitGutenbergRegisterBlock(block) {
525532
*
526533
* @since 2.2.5
527534
*
528-
* @param {Object} props Block properties.
529-
* @param {Object} blockProps Block properties.
530-
* @return {Object} Notice.
535+
* @param {Object} props Block properties.
536+
* @param {Object} blockProps Block properties.
537+
* @param {boolean} buttonDisabled Whether the refresh button is disabled (true) or enabled (false)
538+
* @param {Function} setButtonDisabled Function to enable or disable the refresh button.
539+
* @return {Object} Notice.
531540
*/
532-
const DisplayNoticeWithLink = function (props, blockProps) {
533-
// useState to toggle the refresh button's disabled state.
534-
const [buttonDisabled, setButtonDisabled] = useState(false);
535-
541+
const DisplayNoticeWithLink = function (
542+
props,
543+
blockProps,
544+
buttonDisabled,
545+
setButtonDisabled
546+
) {
536547
// Holds the array of elements to display in the notice component.
537548
let elements;
538549

0 commit comments

Comments
 (0)