react-i18next: Incompatible with hot module reload?
First of all, thank you for this work, š it really looks a lot easier to use than react-intl
which I find a little overkill for a simple app.
I have a translated view that goes like this:
import React from 'react';
import { translate } from 'react-i18next/lib';
const LoginForm = React.createClass({
render() {
const t = this.props.t;
console.log(t); // <-- we should have the translation fn here
return (
<div>
<h3>{ t('Sign in')}</h3>
{/*...*/}
</div>
);
}
});
export default translate(['login'])(LoginForm);
On first load and manual reload it works as advertised, displaying the translated message. The console.log(t)
call returns something like function fixedT(key, options) {...}
, but when I modify something on the component and thus triggering the hot reload module (Webpack), I get undefined
and an Uncaught TypeError: t is not a function
.
Is there any way to make react-i18next compatible with HMR?
Thanks!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 34 (15 by maintainers)
@gnapse you mean if you change something in the translation files you want the hmr to reload your pageā¦not sure if that is possibleā¦as those files are not part of the bundle.
I think the initial issue was a reload based on a change in the componentā¦not translation files.
Personally we moved our translation to the service tier locize.com - so we do no manipulations of translations anymore inside the project repository.
Rather sure there is a way to trigger hmr doing some configurations to watch for the public folder tooā¦but personally i prefer doing create-react-app init - so i might have less knowledge about webpack configā¦eventual this is a good question for stackoverflow?
I got it working. Finally. Via this post: https://github.com/i18next/react-i18next/issues/3, I was mentioned this post: https://github.com/ghengeveld/redux/blob/a68768ff8d6a4fbe7b8f6a06a8cf9b99a54aefb0/docs/recipes/WritingTests.md#testing-decorated-react-components
Basically, because in order to test the undecorated component and not the decorated translate component, I had to export the component using:
export class ComponentName extends Component { ... }
next to theexport default translate('common')(ComponentName)
at the bottom. Because I export the undecorated component next to the decorated component, I can import the undecorated component in my unit test usingimport { FilterRow } from '../components/filterRow';
instead ofimport FilterRow from '../components/filterRow';
Hey guys, thereās a solution.
Put this after i18next.init call.
Where
[assetsModule]
is the module that export the resources, used as resources in the initial option and also for the hot replacement. You can also usei18next-resource-store-loader
.Complete Example:
With this, every time you change a value into resources, the hot reloader replace all the namespaces with new values into i18next and emit
loaded
event.Doing so, all the react component wrapped with the
Translate
component, will be re-rendered.I just ran into this same issue.
Iām using the code as a decorator
Hereās my i18n.js init.
strings/index.js
strings/en.json
And Iām using the json-loader with webpack.