TypeError:无法读取react-localize-redux中未定义的属性'languages'

时间:2019-02-22 04:39:26

标签: reactjs redux

我试图对我的应用程序实现react-localize-redux。但是我出现了以下错误

  

TypeError:无法读取未定义的属性“语言”

这来自 node_modules / react-localize-redux / es / localize.js:249

我的代码实现如下。

1st & 2nd

App.js

3rd & 4th

global.json

let store = createStore(combineReducers({icm: icmReducer, locale: locale}));

ReactDOM.render(
<Provider store={store}>
    <LocalizeProvider store={store}>
        <App/>
    </LocalizeProvider>
</Provider>,
document.getElementById('root'));

错误

enter image description here

3 个答案:

答案 0 :(得分:2)

我找到了解决方案。当我们将组合减速器与localizeReducer一起使用时,我们需要提供名称为 localize 。必须这样做,否则它将不起作用。

import {LocalizeProvider, localizeReducer} from 'react-localize-redux';

let store = createStore(combineReducers({icm: IcmWebReducer, localize: localizeReducer}));

在这里提到:https://ryandrewjohnson.github.io/react-localize-redux-docs/#what-if-i-want-to-use-redux

答案 1 :(得分:0)

看起来您已经初始化了2种语言,但翻译了3种。您应该将西班牙语添加到初始化程序中:

languages: [
  {name: "English", code: "en"},
  {name: "French", code: "fr"},
  {name: "Spanish", code: "es"}
],

(只是猜测这是问题所在)

答案 2 :(得分:0)

如果您想在商店中使用不同名称或不同路径的localize,只需使用getState属性。

(TypeScript)

<LocalizeProvider
  store={store}
  getState={(state: AppState) => state.user.locale}
>
//...
</LocalizeProvider>

应用状态的结果:state.png