user567
user567

Reputation: 3872

Access JSON data with react

I have a JSON in this form

"description": "{\"en\":\"some text\",\"fr\":\"un autre text\"}",

I can access the english text in this way

 <Cell cellTitle='test' cellDescription={JSON.parse(rowData.description).en}

but when I use a variable that contains the current language, it dosen't works. I tried this

  render() {
     const lang = I18n.locale;
    return (
<Cell cellTitle='test' cellDescription={JSON.parse(rowData.description).lang}

or

<Cell cellTitle='test' cellDescription={JSON.parse(rowData.description).{lang}}

none of them worked. Can you help me solve the problem?

Upvotes: 0

Views: 94

Answers (1)

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19069

This is how you do it:

render() {
  const description = JSON.parse(rowData.description);
  const lang = I18n.locale;

  return (
    <Cell cellTitle='test' cellDescription={description[lang]} ... />
  )
}

Upvotes: 1

Related Questions