-
Notifications
You must be signed in to change notification settings - Fork 125
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
Conversation
Thanks for your contribution! Depending on what you are working on, you may want to request a review from a Shopify team:
|
e64e8d7
to
8b9c8e3
Compare
8b9c8e3
to
4486ea6
Compare
Coverage report
Test suite run success1806 tests passing in 822 suites. Report generated by 🧪jest coverage report action from 4486ea6 |
We detected some changes at either packages/*/src or packages/cli-kit/assets/cli-ruby/** and there are no updates in the .changeset. |
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.
🎩 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)) |
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.
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".
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.
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.
#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?
app logs
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:
Checklist