-
Notifications
You must be signed in to change notification settings - Fork 94
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
feat: implement useSsrState
hook
#1023
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1023 +/- ##
=======================================
Coverage 99.61% 99.61%
=======================================
Files 61 62 +1
Lines 1041 1048 +7
Branches 163 163
=======================================
+ Hits 1037 1044 +7
Misses 2 2
Partials 2 2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
I will have time to review this on Friday. I have two questions about this:
|
|
But there will be an issue with typing, that I have no idea how to overcome |
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.
Looks good to me. So basically this is a boolean context that the isomorphic hooks read to know whether to initialize with a value or not.
One question though: should we wait until all isomorphic hooks are refactored to use this hook before merging this? Currently this hook does nothing, even though the docs say that it affects other hooks of ours.
The users could use this hook to build their own isomorphic hooks though. Maybe that should be mentioned in the docs too?
Before merge we have to figure out how to make proper types for isomorphic hooks. As for now - it is pretty easy to alter return types basing of function parameter, but in case of switching to this hook - it is pretty much impossible to type it properly (at leas in my mind, research needed) |
This is very tricky. I wanted throw a suggestion out there: What if we split the Basically, thinking back to #1000 there'd be a |
It will simply double the amount of supported code and will lead to issues when developer uses different hooks in different places. Linter for this case is weak excuse. |
What i'm thinking now - we can offer type extension for SSR users, and basing on that extension all types for SSR-sensitive hooks will switch to SSR-compatibility mode. |
does something like this help? https://twitter.com/benlesh/status/1610298056540397568?s=46&t=Ar1ixjc9BhyFJS15ooM1Fg |
Nah, it is not a problem to set state - problem is in typings of hooks return. |
I got hung up on the idea that we could conditionally use a different hook for #1000 and maybe define the initial value within only Ignore me. |
Just as an idea: So basically it will require to redeclare some types, along with such hook (that i'm planning to migrate from conext to globally stored variable) -thus youll have a single change to make all compatible hooks to switch to SSR state |
That still only adjusts the types, no? Won't all client-only users suffer from an extra render and bundled code? |
Thre is basically no difference, afaik, since i don believe current |
hmmm the types don't solve the bigger DX problem which is that depending on your apps context, you'd need to provide arguments to certain hooks 100% of the time. Maybe we can do like a top-level React-Hookz Provider where we can give ourselves context regarding the users app being client-only or having any server-side code? I think this would still mean conditional code though in a way you've previously pointed out has problems. Alternatively, we could go full steam ahead and client-only users will just suffer the inconvenience. |
This pr brings such context value to switch all hooks it one move |
lol yes it does 🤦🏼 ! sorry i was stuck thinking about that old PR and not this PR. then yes, i love it in conjunction with the TS idea. seems like the best solution. |
It recently occurred to me that the new It allows components to subscribe to external stores (in our case, browser APIs) and make React aware of their changes. The hook has a parameter called Any thoughts? |
As an experiment, I attempted to port However, our toolchain would need some upgrading. The React teams has provided a package called Perhaps it would be time to reconsider which React versions we support? |
Sadly had a few time lately, therefore havent tried that hook. I'll try to cut some time this week to have a closer look. |
@ArttuOll i just rememebered why we still not using react 18 and its hooks =) the teting library do not support react 18, the solution they offer is switching to |
What new hook does?
Provides stable value that other hooks can use to determine that applocation is in SSR mode.
Sad enough there are no reliable markers that application is hydrated after SSR - therefore we have to introduce own context.
Checklist