【问题标题】:react-localize-redux cannot read property 'map' of undefinedreact-localize-redux 无法读取未定义的属性“地图”
【发布时间】:2019-02-25 15:55:06
【问题描述】:

我正在尝试使用 react-localize-redux 从 JSON 文件加载翻译,但我不断收到此错误。这对我来说都是全新的,所以如果这是显而易见的事情,我深表歉意。从阅读文档中我可以看出这“应该”有效吗?

我收到的错误消息:

translate.js

import { combineReducers, createStore } from 'redux'
import { localizeReducer, initialize, addTranslationForLanguage, getTranslate } from 'react-localize-redux'
import translationsEn from '../../../nls/en.json'

const localeStore = createStore(combineReducers({
  locale: localizeReducer
}))

const languages = ['en']
localeStore.dispatch(initialize(languages))
localeStore.dispatch(addTranslationForLanguage(translationsEn, 'en'))

export default getTranslate(localeStore.getState().locale)

在我的组件中:

import translate from '../state/translate/translate'

...

<span className='node-output-schema__title'>{translate('outputSchema.title')}</span>

对可能出现的问题有什么想法吗?

【问题讨论】:

  • 可能是 { translate(['outputSchema.title']) },但不确定
  • localize.js 中有什么

标签: javascript reactjs redux


【解决方案1】:

由于您的属性未定义,因此出现此错误,因此请检查您的错误并获取确切的行(您可以在浏览器的控制台选项卡上找到错误)并检查那里使用的属性并检查您在哪里填写该属性如果您没有填充你的属性然后设置它

【讨论】:

    【解决方案2】:

    好像你在这里混合了一些不同的框架。

    本地化包被称为-react-localize-redux。 但是在您的错误日志中,我可以看到您使用了一些角度。

    另外,我刚刚检查了 react-localize-redux 包中的文档,看来您正在使用一个过时的版本。 对我来说,只为您的应用程序提供一个 Provider 就足够了,然后使用更高阶的组件(import { withLocalize } from "react-localize-redux"; )

    另外我会推荐使用这个包,它更容易处理(实际上我自己用它来做一个项目) react-18next (https://github.com/i18next/react-i18next)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2019-03-19
      • 2019-10-29
      • 2020-07-23
      • 1970-01-01
      • 2019-02-08
      • 2018-05-10
      相关资源
      最近更新 更多