react-i18next icon indicating copy to clipboard operation
react-i18next copied to clipboard

Why is Suspense used twice in the React example?

Open nicholasmaddren opened this issue 7 years ago • 7 comments

I've just took a look at this example:

https://github.com/i18next/react-i18next/blob/master/example/react/src/App.js

I've noticed that Suspense is used within the Page function and the App function uses Suspense as well.

I'm just wondering why the App function needs Suspense when the Page already has it?

I've currently set my project up just like your example using XHR to load my translation files. Any documentation around using i18next-xhr-backend and Suspense would be great.

Thanks, Nick

nicholasmaddren avatar Feb 27 '19 11:02 nicholasmaddren

You're right...might be i accidently added the Suspense in Page when doing some tests...will remove that asap - in the end you can have suspense where needed -> just needs be outside of the hook using function

jamuhl avatar Feb 27 '19 12:02 jamuhl

Awesome, thanks for clarifying that.

nicholasmaddren avatar Feb 27 '19 12:02 nicholasmaddren

What documentation you think of?

In the end useTranslation or any other component loading namespaces via xhr-backend if not set different will throw when not ready triggering the Suspense. So everywhere you load namespaces wrapping around you will need the Suspense component -> if you like that on App, Page, Section level is up to your need.

jamuhl avatar Feb 27 '19 12:02 jamuhl

If you like this module don’t forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project -> there are many ways to help this project :pray:

jamuhl avatar Feb 27 '19 12:02 jamuhl

Great, I understand that now. Thanks for making i18n so easy to implement!

nicholasmaddren avatar Feb 27 '19 12:02 nicholasmaddren

I am fetching namespaces for my i18next from my backend server.

I have a page route to namespaces mapping json file: pageNameSpaceMapping = { '/page1': [ns1, ns2] }

I am loading namespaces by listening to route changes using history.listen((pathname) => i18n.loadNamespaces(pageNameSpaceMapping[pathname]) ).

I also have a Suspense component wrapped at the top of my React app.

But when I navigate to /page2 route, the loadNamespaces get called and I fetch the namespaces, the react component already gets rendered and not the Suspense fallback Component.

My doubts are:

  1. Doesn't the Suspense component not work for loadNamespaces api ??
  2. Is it because I am calling the loadNamespaces from the history.listen handler ??

rajathgunners avatar Aug 29 '21 15:08 rajathgunners

@rajathgunners idk...looks like you build something completely independent of react-i18next -> so the only one able to find out what is wrong - is you...

useTranslation, withTranslation all work completely fine with the Suspense API -> loadNamespace is just a function loading namespaces (there is no connection to react at all)

jamuhl avatar Aug 29 '21 15:08 jamuhl