-
Notifications
You must be signed in to change notification settings - Fork 166
Change useCookie hook to getter, avoid global override #5778
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,15 +44,17 @@ export function chaiConsoleSpy(chai, utils) { | |
| * `chaiConsoleSpy` Chai plugin. | ||
| */ | ||
| export function useConsoleLogSpy() { | ||
| let originalConsoleError; | ||
| beforeEach(() => { | ||
| console.unverifiedCalls = []; | ||
| sinon.stub(console, 'error').callsFake((message, ...args) => { | ||
| originalConsoleError = console.error; | ||
| console.error = sinon.stub().callsFake((message, ...args) => { | ||
| console.unverifiedCalls = console.unverifiedCalls.concat(format(message, ...args)); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| console.error.restore(); | ||
| console.error = originalConsoleError; | ||
|
Comment on lines
+47
to
+57
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't mean to include the changes here, but they are necessary for an upgrade to |
||
| expect(console.unverifiedCalls).to.be.empty( | ||
| `Unexpected console logging: ${console.unverifiedCalls.join(', ')}`, | ||
| ); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
One might argue that we could just keep this in state, since internally calling
setValuewould trigger a re-render. The problem with this is that the value could fall out of sync with multiple instances of the hook (aside: probably want a test case for this). The getter isn't prone to this, since it'll get the value at the time that it's called.Alternatively, we could introduce some simple shared context which makes sure that if the cookie value is updated, all instances of the hook are re-rendered with the new value.
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 seems pretty straightforward, I think it's clearer than using a shared context
Uh oh!
There was an error while loading. Please reload this page.
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.
Added in cd4d91f.
I was curious, so I tried in 1f493d5 to see what this might look like. It's a bit more complex, yeah, though not too bad I think? I guess I'm mostly concerned that the hook may give a false sense that things are kept well in-sync.
Specifically, this assertion would fail without these changes:
identity-idp/spec/javascripts/packages/document-capture/hooks/use-cookie-spec.jsx
Line 64 in 1f493d5
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 that's my main reason for thinking the getter function is better, because it is the closest thing to a "live" value
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.
Problem is that it's live at the time that it's called, but in a case where we use the value as part of the render logic of a component, only the component instance which calls the setter would trigger an re-render, and any other component instance would continue showing a stale value.
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.
oh right I forget how "lazy" react is 😩 happy to trust your judgement on what is worthwhile vs overkill for something like this
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 wish it were easier to manage a "global" value than juggling subscribers like this, but I think it's fine enough. I'd also like to limit our use of "force render" vs. built-in alternatives like
setState, so I feel a bit happier with this approach.