-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
FEEDBACK WANTED FOR NEXT MAJOR VERSION: using HOOKS and SUSPENSE #590
Comments
at first glance, it seems very straightforward. I'd say, however, to use |
@schettino totally agree will update that on the next release to |
Again taking a quick glance - this looks great. Is it fair to say this is a 1:1 rewrite, using hooks, without any other kind of refactoring? There are some things going on I don't understand (like throwing a Promise), but the actual usage of hooks looks correct to me. Is it true that you can't pass functions into |
Yes, not being able to pass a function, dan already mentioned that this is a problem on twitter - i guess that will be sorted out at some point (for now it's an "acceptable" workaround). Yes, there is not much functionality removed (beside not using context api, removing the render prop - which could easily be added in userland if needed) - just need to figure out the SSR part again. Not yet sure if by adding an explicit hook, Component or by just passing a second argument to the |
@isaachinman throwing the Promise -> that is how Suspense works under the hood...it catches and if it's a promise -> showing the fallback. When all promises resolve it goes back and renders the actual content...a little weird but seems to work 🙄 |
Ah, I didn't see that tweet. As long as they're aware of it, and are treating it like a bug, no problem. Also strange to hear that about suspense. I watched Dan's talk awhile back but haven't gotten hands-on with it yet. Throwing a promise seems like a really weird API, but I guess good to know! Maybe you can explain a bit more about what we need to get SSR working again? In my opinion a minor-bump-refactor is not worth it if it means dropping crucial functionality like SSR. Is it your plan to get SSR working before merging this? It sounds like you have a few possible approaches in mind. We can discuss if it is helpful to you. |
@isaachinman not yet sure if there also will be some overhaul on next.js itself...but for sure SSR will need to work for v10. guess reportNS could be implemented as a "global" variable inside the hook - so we fill that under the hood without extra pass reportNS -> and expose that on i18n or similar. Than it's just about handling the initialStore/Lnaguage which in my opinion should be done expicit in a separate SSR hook or component. |
Are we discussing SSR with NextJs, or in general? I don't really understand your point about |
both next.js will be the focus...but should be helpful on SSR using razzle or whatever too: -> https://github.com/i18next/react-i18next/blob/master/src/hooks/context.js#L59 is usable tor razzle too (same for the withSSR hoc) |
A complication I notice when trying out both hooks and suspense at the same time is that suspending a component will throw out the work in progress hooks. If you want to suspend as part of implementing Based on recent commit activity I've seen it's possible it's a synchronous mode only issue as well, but I haven't tried asynchronous mode. What I mean is, mounting this component will enter an infinite loop, always recreating the ref and the promise on every resume because their work in progress state was discarded: function UsesResource(props) {
const resourceRef = useRef(undefined)
const resourcePromise = useMemo(async () => {
resourceRef.current = await import('./resource')
}, [])
if (resourceRef.current === undefined) {
throw resourcePromise
}
render <>{/* stuff using resourceRef */}</>
} |
@Kovensky there is nothing like work in progress for i18next -> it get's triggered to load and stores all translations in it's own "store" -> so even discarding a component won't stop loading and won't trigger a reload. the hook works like: call the hook first time: are needed namespaces loaded? -> yes: go on and return values |
Ah, right, you can avoid the problem by not having the thing storing the data you care about being inside this component 🤔 |
Hello Jan. What do you think about switching for a full name also for such case:
Using Actually, in my project, I am using full name |
Jan, Excuse me if I will tell a nonsense now, but seems current implementation of hooks does not cover such weird scenario (when different parts of the application are localized with different instances of i18n):
I understand that keeping
Thanks a lot, you are doing a great job with i18next. |
@vict-shevchenko that is right...the option to have multiple i18next instances gets dropped....but honestly that is a rather large edge case of using multiple instances -> in react-intl it's the only way to use multiple translation files - as i18next comes with namespaces the only use case having multiple instances (at least the only i see) is having differently formatted translation files. What we might do is adding a get i18n from props here: https://github.com/i18next/react-i18next/blob/master/src/hooks/useTranslation.js#L26 or some other way to inject getting the i18n instance (eg. by using useContext) - so I18nextProvider and using react context API could be done outside of react-i18next for projects needing those multiple instances - what do you think? |
Hi, Jan! Excuse me for not responding so long. Yet, had not changed to think through the approach. For sure, I am not aware of how often there is a scenario with multiple instances, and this is highly possible that people can solve their task in another way. (staying with one instance) Regarding a solution for passing i18next instance - definitely keeping this logic out of react-i18next will do the job. 👍 I thought if we can somehow omit having own logic for storing an i18next instance and use React Context for it, as it was like designed for such purposes. But I really can not come up with a solution that keeps code as simple as it is now. :( I also see that some library authors are getting rid of |
I'm trying to get this to work using next.js and SSR In order to get this to work, I have to import Otherwise I get the following error:
|
@DmacLoyaltyOne that's for now...when we release v10 we will set the entry points in package.json to the correct build. Currently |
Love the hooks feature. Using it on two projects already. Is it too early to be posting bugs? useTranslation(['ns1', 'ns2']) doesn't seem to merge in the second namespace. |
@cthurston no not to early - quite the opposite - just go ahead and open those issues...just add somewhere that it's about the hooks version ;) |
seems hooks works pretty well for me. Lack of SSR example with hooks was a problem :). |
@gorkunov thank you for the feedback. we are aware of that and next.js sample is currently a open task: #591 (comment) if time comes i'm sure @isaachinman will update next-i18next with it. |
Yes, I'll be updating |
closing this - seems all currently needed is in the v10 "roadmap" issue |
We are rather amazed at how much smaller and easier react-i18next feels using hooks and suspense.
=> Early Feedback would be very welcome 🙏
A little introduction: https://react.i18next.com/experimental/using-with-hooks
sample code: https://github.com/i18next/react-i18next/tree/master/example/react-hooks
new source code: https://github.com/i18next/react-i18next/tree/master/src/hooks
The text was updated successfully, but these errors were encountered: