Umar745
Umar745

Reputation: 346

Insert React state variable into rendered array

How do I insert the "word" variable into the rendered object as in the code below I have tried the $word and $word but none works

import React, {useState, useEffect} from 'react'

export default function dictionary() {
  const data = {
    "ABATE": {
      "MEANINGS": {
        "2": [
          "Verb",
          "become less in amount or intensity",
          [ "Decrease", "Diminish", "Lessen", "Fall" ],
          [ "The storm abated", "The rain let up after a few hours" ]
        ]
      },
      "ANTONYMS": [],
      "SYNONYMS": [ "Slack off", "Slack", "Abate", "Die away", "Slake" ]
    },
    "ABATEMENT": {
      "MEANINGS": {
        "2": [
          "Noun",
          "the act of abating",
          [ "Moderation", "Mitigation" ],
          [ "laws enforcing noise abatement" ]
        ]
      },
      "ANTONYMS": [],
      "SYNONYMS": [ "Abatement", "Suspension", "Reprieve", "Hiatus", "Respite" ]
    },
  };
  const [word, setWord] = useState('ABATE')

  return ( 
    <>
      <div>
        <label>Word</label>
        <input type="text" value={word} onChange={(e) => setWord(e.target.value)} />
      </div>
      <div className="container pt-n5" >
        <div className="jumbotron d-flex justify-content-center " >
          <div>
            <b> Meaning:</b>
            <p>{data.$`word`?.MEANINGS[0]}</p> {word} <br />
            <b> Meaning:</b>   <p>{data.$word?.MEANINGS[1]}</p>
            <b> Meaning:</b>   <p>{data.$word?.MEANINGS[2]}</p>
          </div>
        </div>
      </div>
    </>
  );
}

Upvotes: 1

Views: 26

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28424

Using the bracket-notation:

data[word]

Upvotes: 1

Related Questions