-
-
Notifications
You must be signed in to change notification settings - Fork 260
Add forceScope parameter to useUnit in react #776
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
Add forceScope parameter to useUnit in react #776
Conversation
👷 Deploy request for effector-docs pending review.Visit the deploys page to approve it
|
igorkamyshev
left a comment
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.
Sounds great to me 💙
Folks, @effector/core what do you think?
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.
Hi @sagdeev !
Thanks for contribution!
Everything looks good, but there is a bit of misunderstanding, probably:
In the original issue useUnit without forceScope must try to find the scope from context and if scope is not found - work without it
So overall there a few things missing:
- Last test snapshot with
forceScope: falseshoud have a value from scope - Need to add another test with
forceScope: falseand without Provider (should work like it currently works) - Need to add isomorphic behaviour
|
|
||
| expect(container.firstChild).toMatchInlineSnapshot(` | ||
| <div> | ||
| 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.
Hmm, looks like there is a bit of misunderstanding 🤔
In the original issue #765 there is a requirement for an isomorphic behavior, which basically means useUnit without forceScope will try to get scope from context and if its not found -> work like there is no scope
So this test should be updated this way:
| value | |
| scoped value |
src/react/nossr.ts
Outdated
| export function useUnit(shape) { | ||
| return useUnitBase(shape) | ||
| export function useUnit(shape, opts?: {forceScope?: boolean}) { | ||
| return useUnitBase(shape, opts?.forceScope ? getScope() : undefined) |
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 suggest to add isomorphic behaviour here something like this:
| return useUnitBase(shape, opts?.forceScope ? getScope() : undefined) | |
| return useUnitBase(shape, getScope(opts?.forceScope)) |
src/react/ssr.ts
Outdated
| export function getScope() { | ||
| const scope = React.useContext(ScopeContext) | ||
| if (!scope) |
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.
It looks like a simplest place to introduce isomorphic behavoir to me, but maybe there a better ones 🤔
I think, it may look like this:
| export function getScope() { | |
| const scope = React.useContext(ScopeContext) | |
| if (!scope) | |
| export function getScope(forceScope?: boolean) { | |
| const scope = React.useContext(ScopeContext) | |
| if (forceScope && !scope) |
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 think we need to move getScope out from the ssr.ts file.
Because all export from this module is available to the end-user.
|
@AlexandrHoroshih hi! I think I understand what you were talking about 😃 I made some changes as you requested. Can you please take a look?
|
AlexandrHoroshih
left a comment
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.
@sagdeev Awesome, looks perfect 🔥👍
Conventions
Resolves #765