[英]Cannot read property of “undefined” even though the object IS defined! [React]
I am trying to generate the markup for a dropdown menu that will allow users to change the language/locale of the site. 我正在尝试为下拉菜单生成标记,该标记将允许用户更改网站的语言/区域设置。
But my component will not render because the browser is telling me the object (language) I'm trying to pull country names (locales) off of is undefined. 但是我的组件无法渲染,因为浏览器告诉我我试图提取国家名称(语言环境)的对象(语言)是未定义的。
What I don't understand is that when I console log that same language object and the key that I'm passing to it (selectedLang) in the lines above, they both print out just fine. 我不明白的是,当我在上面的行中控制台记录该语言对象和要传递给它的键(selectedLang)时,它们都可以正常打印。
Here is my code: 这是我的代码:
compileAvailableLocales() {
let locales = availableLangs;
let selectedLang = this.props.lang;
console.log("selectedLang: ", selectedLang, typeof selectedLang);
console.log("availableLangs: ", availableLangs);
console.log("language: ", language);
let markup = locales.map( (loc) => {
let readableName = language[ selectedLang ].navigation.locales[ loc ];
return (
<li
key={ loc }
value={ loc }
onMouseDown={ this.handleLangChange }>
{ readableName }
</li>
);
});
return markup;
}
I think the problem may be the way that I am importing the language object from my essential_lang.js file. 我认为问题可能是我从essential_lang.js文件导入语言对象的方式。
I'm importing into the index.jsx file (see code above) with this line: 我使用以下行将其导入index.jsx文件(请参见上面的代码):
import { language } from '../../constants/essential_lang'
And here is some of the code from that file: 这是该文件中的一些代码:
export const language = {
'de_de': {
footer: {
links: [
{
title: 'KUNDENSERVICE',
endpoint: 'mailto:kundenservice@spinmaster.com'
},
{
title: 'Datenschutz',
endpoint: '/de_de/privacy'
},
{
title: 'Impressum und Allgemeine Geschäftsbedingungen',
endpoint: '/de_de/terms'
}
],
copyright: 'Spin Master Ltd. All Rights Reserved.'
},
fourohfour: {
heading: 'Oh Noes!',
message: 'This page doesn\'t exist.',
gohome: 'Back Home'
},
loading: "Loading",
navigation: {
menuitems: [
{
title: 'Videos',
endpoint: '/de_de/videos/'
},
{
title: 'Colleggtibles',
endpoint: '/de_de/colleggtibles'
},
{
title: 'Produkte',
endpoint: '/de_de/toys/'
},
{
title: 'Hilfe/Häufig gestellte Fragen',
endpoint: 'https://spinmastersupport.helpshift.com/a/hatchimals/?p=web&l=de'
},
{
title: 'Eltern',
endpoint: '/de_de/parents/'
},
{
title: 'Spiele',
endpoint: '/de_de/activities/'
},
{
title: 'Wo erhältlich',
endpoint: '/de_de/where-to-buy/'
}
],
locales: {
de_de: 'Deutschland',
en_au: 'Australia',
en_ca: 'Canada',
en_uk: 'United Kingdom',
en_us: 'United States',
es_es: 'España',
es_mx: 'México',
fr_fr: 'France',
it_it: 'Italia',
nl_nl: 'Dutch',
tr_tr: 'Türkiye'
}
}
},
'en_au': { ...
Here is output for the console.logs: 这是console.logs的输出:
Is it as simple as returning a string? 它像返回字符串一样简单吗?
return "
<li
key={ loc }
value={ loc }
onMouseDown={ this.handleLangChange }>
{ readableName }
</li>
";
Difficult without seeing all the code. 很难看到所有代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.