简体   繁体   English

如何突出特定<li>元素?

[英]How to highlight specific <li> element?

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:我正在获取一些看起来像这样的 .json 数据:

[ {
"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 (ie bold) but i don't know how to tell react what li element should be highlighted....现在我想突出显示正确的答案(即粗体),但我不知道如何告诉反应应该突出显示什么 li 元素....

<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格雷茨

you can alse add corrent into props.您也可以将 corrent 添加到道具中。

<ol className="answers-list">
 {
    props.answerOptions.map((answer, index) => (  
       <AnswerDetails 
          key={answer}
          answer={answer}
          index={index}
          corrent={props.correctAnswer}
        /> 
    ))
  }
</ol> 
import './AnswerDetails.css';
import classNames from 'classnames';

const AnswerDetails = (props) => {
  return <li className={classNames({corrent: props.correctAnswer === props.index})>
   {props.answerOptions}
  </li>;
};


export default AnswerDetails; 

Just loop over the items in the array and map them to styled <li> elements.只需遍历数组中的项目并将它们映射到样式化的<li>元素。

 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>

Remember javascript arrays are zero-based indexed.请记住,javascript 数组是从零开始索引的。 Assuming "correctAnswer" corresponds to the ordered-list of "answerOptions", you can write it like this.假设“correctAnswer”对应于“answerOptions”的有序列表,你可以这样写。

answers.json

{
  "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
    }
  ]
}

import json from "./answers.json";

const { answers } = json;

export default function App() {
  return (
    <div>
      <AnswerDetails answers={answers} />
    </div>
  );
}

const AnswerDetails = (props) => {
  return (
    <ul>
      {props.answers.map(({ answerOptions, correctAnswer }, index) => (
        <AnswerComponent
          key={index}
          options={answerOptions}
          correctAnswer={correctAnswer}
        />
      ))}
    </ul>
  );
};

const AnswerComponent = (props) => {
  return (
    <ol>
      {props.options.map((option, index) => (
        <li
          key={option}
          style={{
            fontWeight: props.correctAnswer - 1 === index ? 700 : "initial"
          }}
        >
          {option}
        </li>
      ))}
    </ol>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM