react-i18next: [error/warning]: You will need pass in an i18next instance by using initReactI18next
Sorry to bother, I posted this question first on SO
I am using i18next and react, and locally it works as expected, but when I deploy on Surge I always get this error:
react-i18next:: You will need pass in an i18next instance by using initReactI18next
They way I implemented it is almost identical to the react-i18next documentation.
main.jsx
import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import 'tabler-react/dist/Tabler.css'
import { BrowserRouter as Router } from 'react-router-dom'
import App from './App/container'
import i18next from './services/app/i18n'
// import './services/app/i18n' /* eslint-disable-line no-unused-vars */
import initStore from './initStore'
import '../sass/style.scss'
import 'dropzone/dist/dropzone.css'
import 'c3/c3.css'
const store = initStore()
// console.log('i18next', i18next) // if I uncomment this, it works on test server
ReactDOM.render(
<Provider store={store}>
<Router>
<App />
</Router>
</Provider>,
document.getElementById('root'),
)
services/app/i18n.js
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import Fetch from 'i18next-fetch-backend'
import * as apiConfig from '../../../config/api'
import { transformTranslationsData } from './translation-helpers'
const apiEnv = apiConfig[API_ENV]
i18n
.use(Fetch)
.use(initReactI18next)
.init({
// debug: true,
lng: 'en',
keySeparator: false,
fallbackLng: {
default: [ 'en' ],
},
interpolation: {
escapeValue: false, // react already safes from xss
},
react: {
// wait: true,
useSuspense: false,
},
backend: {
loadPath: `${apiEnv.api}/translations?limit=0&locale={{lng}}&key=cm.*`,
allowMultiLoading: true,
parse: transformTranslationsData,
},
})
export default i18n
The thing I don’t understand is that when I console.log the i18next
variable in main.jsx
, it prints out the initialised object, and it works when deployed. If I don’t log it, it just shows the error and the translations are not available in the app.
I am using these versions:
"i18next": "^15.1.0",
"i18next-fetch-backend": "^2.2.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-i18next": "^10.9.0",
It is going to be tricky for me to make a small repo that reproduces this error. For now I am hoping on clues and suggestions so I can investigate further.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 25 (4 by maintainers)
For getting rid of Jest warnings easier than modifying components themselves:
You can place it at the top of the file right after file imports and before the describe block
on the off chance that someone else runs into this, the error had nothing to do with i18next, and it was merely a faulty webpack setting.
In package.json we had:
the issue got resolved by changing it into:
In my case, the warning appeared during jest tests. For the time being, I’ve gotten around it by redefining
t
if in a test:Can someone explain where (which file) we need to write 👇
Do we need to have it within our test.js file or component.js file?
can’t remember where I found this but it works really nice
src/__mocks__/react-i18next.js
sometimes it helps to talk about the issue - or in the worst case some distance to the problem and solutions come up
create a
src/setupTests.ts
with the following:@DevScissors where do you import the i18n.js?!?
The implementation is not:
Get i18n from useTranslation and pass it to the I18nextProvider. The I18nextProvider would get it from the import and would be the most outer Element (before getting t, i18n from useTranslation). The Provider is only needed if not calling
.use(initReactI18next)
.I am still getting a warning about i18nextReactModule in my App.test.js. I had more errors but researched and found this thread and added I18nextProvider to fix a few of them. Below are the files I am working with. Any help would be appreciated.
i18n.js
App.js
App.test.js
UseTranslation.js
UseTranslation.test.js
@VisheshSingh @rogger-frogger
jest allows to define a set of setup files to handle this kind of stuff
In create-react-app this file is called setupTests.js
For those who use Trans component, it makes sense to mock it as well:
Otherwise there will be an error of “Invariant Violation: Element type is invalid”.
Maybe i’m overlooking something here, but for me the fix was a mere one-liner. Simply added
to the
config
-array intoweb/jest.config.js
and the warning went away. (web/src/i18n.js
is the init-script also used inApp.tsx
)Of course, one could define a separate file initialization script specifically for the test environment and/or write a hook-mock to be executed before each test. The latter would then be configured to be made available to every test with
setupFilesAfterEnv
i guess.Unfortunately it didn’t bring me much further, I am starting to wonder if webpack is just ignoring the whole
services/app/i18n.js
file.I placed a console.log in the
services/app/i18n.js
file above the init call, and a log in theparse: transformTranslationsData,
function. When running on the server:The error still shows, despite adding
initImmediate : false
to the init options And thetReady
prop istrue
right away, Neither log gets called. Checking the heroku api logs, the translations api call is never made.If I run it locally,
tReady
is firstundefined
, then I see the parse function called, and thetReady
is true. And the translation are there.edit I don’t know why I am telling you all this 😄, I’ll close the issue, thanks for your time
Thanks for the example @FDiskas.
It’s from here https://github.com/i18next/react-i18next/blob/7cfab9746b3ccc6f833cd9c892e7c3c804944a5e/example/test-jest/src/__mocks__/react-i18next.js.
Extending it for the case of handling objects:
It’d be: