How to highlight specific <li> element?

Marc -

i am trying to learn react at the moment and i can't find a solution for following problem:

I am fetching some .json data which look like that:

[ {
"answerOptions": [
  "Answer A",
  "Answer B",
  "Answer C",
  "Answer D",
],
"correctAnswer": 1
},
{
"answerOptions": [
  "Answer A",
  "Answer B",
  "Answer C",
  "Answer D",
  "Answer E",
  "Answer F",
],
"correctAnswer": 4
}, {..}, {..} ]

Now i want to highlight the correct answer (i.e bold) but i don't know how to tell react what li element should be highlighted....

<ol className="answers-list">
 {
    props.answerOptions.map((answer) => (  
       <AnswerDetails 
          key={answer}
          answerOptions={answer}
        /> 
    ))
  }
</ol> 
import './AnswerDetails.css';

const AnswerDetails = (props) => {

    return (
      <li>
       {props.answerOptions}
      </li>
     );
 }

export default AnswerDetails; 

Maybe someone of you has a small hint for me :)

Greetz

Mr. Polywhirl

Just loop over the items in the array and map them to styled <li> elements.

const { useState } = React;

const answers = [{
  "answerOptions": [
    "Answer A",
    "Answer B",
    "Answer C",
    "Answer D",
  ],
  "correctAnswer": 1
}, {
  "answerOptions": [
    "Answer A",
    "Answer B",
    "Answer C",
    "Answer D",
    "Answer E",
    "Answer F",
  ],
  "correctAnswer": 4
}];

const AnswerDetails = ({ answerOptions, correctAnswer }) => (
  <li>
    <ol className="answers">
      {answerOptions.map((answerOption, index) => (
        <li
          key={answerOption}
          className={index === correctAnswer ? 'correct' : ''}
        >
          {answerOption}
        </li>
      ))}
    </ol>
  </li>
)

const Answers = ({ answers }) => (
  <ol className="question">
    {answers.map(({ answerOptions, correctAnswer }, index) => (
      <AnswerDetails
        key={JSON.stringify(answerOptions)+correctAnswer}
        correctAnswer={correctAnswer}
        answerOptions={answerOptions}
      />
    ))}
  </ol>
);

const App = () => (
  <div>
    <Answers answers={answers} />
  </div>
);

ReactDOM
  .createRoot(document.getElementById("root"))
  .render(<App />);
.question { list-style-type: decimal-leading-zero; }
.answers { list-style-type: upper-alpha; }
.answers { margin-bottom: 1em; }
.correct { font-weight: bold; color: green; }
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.1.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.1.0/umd/react-dom.development.js"></script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if an <li> element has a specific value

Styling specific <li> element in unordered list <ul>

How to hide a specific text inside a <li></li> element using css?

Click on li element searching specific displayed text

Solr - How to highlight specific terms in specific fields

JS delete specific <li> element by addEventListener()

How to HIGHLIGHT a specific popup menu item?

How do I add a class to a specific li element with React?

How to highlight specific ranges in various boxplots in R?

Apply CSS style only to specific li element

How to highlight text of specific letters in Flutter?

How to highlight element in selenium webdriver

How to target a specific <li>

How to highlight specific string in magento

How to highlight specific rectangles on canvas

How can I get the li-Element in "Angular2 for TypeScript" (beta) for adding specific CSS class?

How to align specific item into a <li>

How to highlight a specific word in a searched content in wordpress

How to delete specific li element in jQuery

DOMXPath Query To Extract Specific li Element

How to highlight search text in DataGridView in specific column?

How to get specific ul element li's text and href having no any class or id using beautifulsoup

How to get text in a specific <li> element in jQuery?

MarkLogic: cts:highlight with specific element

Pseudo class :before selectors in specific <li> element

How would I get multiple "li" elements under a specific "ul" element and put them into an array?

How do I remove one specific <li> element from html using basic javascript?

How to highlight only a specific div onclick in reactjs?

Need help personalizing my CSS navbar: how do I highlight the <li> element on mouseover?