I'm new to React and I can't figure out how I can call a function inside through onClick() on a map loop. Let me show you:
const changeLang = lang => {
i18n.changeLanguage(lang);
};
I have this function who is responsible to get me languages like English or Portuguese. And what I'm trying to do with the following code is change the language depending on which menu item I choose from a menu. To do this I have the following code:
const languages = [
{
value: 'pt',
label: 'Portuguese',
},
{
value: 'en',
label: 'English',
}
];
...
{languages.map(option => (
<MenuItem key={option.value} value={option.value} onClick={() => this.changeLang (option.value)}>
{option.label}
</MenuItem>
))}
The problem is that it can't enter in my function changeLang
, so the language is not chosen, no effect at all. I've tried the console.log and I get nothing in return. I'm pretty sure the problem is on onClick={() => this.changeLang (option.value)}
, but I can't figured out why. Can you help me?