Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only reset errors if there are errors #4282

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

andrewhassan
Copy link
Contributor

@andrewhassan andrewhassan commented Aug 7, 2024

#gsd:39776

WHY are these changes introduced?

Fixes https://github.com/Shopify/shopify-functions/issues/364

WHAT is this pull request doing?

Because React re-renders when a state changes (by reference), every time we received a successful request, we were setting the errors array. We poll roughly twice a second, which meant that we are performing a ton of unnecessary re-renders, causing flickering.

How to test your changes?

  1. Run app logs
  2. Trigger enough function runs so that your terminal has to scroll

With this change, there should be no flickering and it should be easy to copy your function run output.

Measuring impact

How do we know this change was effective? Please choose one:

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
  • Existing analytics will cater for this addition
  • PR includes analytics changes to measure impact

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

Copy link
Contributor

github-actions bot commented Aug 7, 2024

Thanks for your contribution!

Depending on what you are working on, you may want to request a review from a Shopify team:

  • Themes: @shopify/advanced-edits
  • UI extensions: @shopify/ui-extensions-cli
    • Checkout UI extensions: @shopify/checkout-ui-extensions-api-stewardship
  • Hydrogen: @shopify/hydrogen
  • Other: @shopify/app-management

Copy link
Contributor

github-actions bot commented Aug 7, 2024

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
72.62% (+0.11% 🔼)
7914/10898
🟡 Branches
69.4% (+0.06% 🔼)
3887/5601
🟡 Functions
71.34% (+0.04% 🔼)
2078/2913
🟡 Lines
72.95% (+0.12% 🔼)
7478/10251

Test suite run success

1806 tests passing in 822 suites.

Report generated by 🧪jest coverage report action from 4486ea6

@andrewhassan andrewhassan marked this pull request as ready for review August 7, 2024 21:51
Copy link
Contributor

github-actions bot commented Aug 7, 2024

We detected some changes at either packages/*/src or packages/cli-kit/assets/cli-ruby/** and there are no updates in the .changeset.
If the changes are user-facing, run "pnpm changeset add" to track your changes and include them in the next release CHANGELOG.

@andrewhassan andrewhassan requested review from mssalemi, lopert and DuncanUszkay1 and removed request for mssalemi August 7, 2024 21:51
Copy link
Contributor

@lopert lopert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎩 worked for me, didn't see any flickering and was able to scroll and select

@@ -52,7 +52,7 @@ export function usePollAppLogs({initialJwt, filters, resubscribeCallback}: UsePo
}
retryIntervalMs = result.retryIntervalMs
} else {
setErrors([])
setErrors((errors) => (errors.length ? [] : errors))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really get how this fixed the issue?
Before, we were always setting the state to [] which caused the refresh.
Now, we're either setting the state to [] or errors, either of which should cause the refresh again?

I did some searching of the useState docs, but it's not clear to me if providing a "updater" function also gets us a behaviour of "if the state hasn't changed, then don't update it".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React re-renders if a state changes, and the way it determines if the state changed is by reference. When we setState([]), we're setting the state with a new array. Although the contents are the same, the reference is not, so React re-renders, causing the flickering.

To avoid this, we want to reset the errors only if there are already errors. Ideally, we'd do something like this:

if (errors.length) {
  setErrors([])
}

But the problem with that is that errors inside this function actually refers to the initial errors array, not the current value! So we can't simply reset the errors based on the current errors value.

Luckily, React exposes the current value through the setter updater function. I use that current value to decide what we should do. If there are errors, then we set the state to [], triggering a re-render. If there aren't errors, we set the state to the exact same reference, which does not trigger a re-render.

@andrewhassan andrewhassan added this pull request to the merge queue Aug 8, 2024
Merged via the queue into main with commit 3148623 Aug 8, 2024
37 checks passed
@andrewhassan andrewhassan deleted the ah.fix-app-logs-rerendering branch August 8, 2024 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants