怎样检查哪个数组元素是正确或错误,并更改组件颜色?

huangapple go评论70阅读模式
英文:

How can I check which array element is correct or wrong and change component color?

问题

我的问题是,当我检查问题的替代选项哪一个是正确或错误时,当我点击其中一个按钮时,它会正确工作,给出问题的情况颜色(是否正确或错误),但它为所有按钮呈现颜色,而不仅仅是用户点击的那一个。

我创建了一个**data**文件

```typescript
export const Questions = [
  {
    alternatives: [
      { answer: "OPT1", isCorrect: 1, alternative: 'A' },
      { answer: "OPT2", isCorrect: 2, alternative: 'B' },
      { answer: "OPT3", isCorrect: 2, alternative: 'C' },
      { answer: "OPT4", isCorrect: 2, alternative: 'D' },
    ]
  }
]

错误示例:第一个替代选项是正确的,当我点击它时,所有按钮的颜色都变成了绿色,当替代选项不正确时也是如此(但为红色)。我想要的是只改变点击的按钮的颜色,而不是所有按钮的颜色。

在我的Question Modal文件中,我正在检查问题的情况并给出颜色。我首先导入之前的数据文件,然后创建状态和条件函数

const questions = Questions ?? [];
const [isCorrectQuestion, setIsCorrectQuestion] = useState<number>()
const test = (isCorrect: number) => {
  if (isCorrect === 1) {
   setIsCorrectQuestion(1)
  }
  else if (isCorrect === 2) {
   setIsCorrectQuestion(2)
  }
}

最后,在渲染替代选项时,我正在映射我的data数组并使用style属性来检查哪个替代选项是正确的或错误的

{questions[0].alternatives.map(e =>
  <Styles.Button
    onClick={() => test(e.isCorrect)}
    style={{
      backgroundColor:
        isCorrectQuestion === 1 ? 'green'
        : isCorrectQuestion === 2 ? 'red'
        : 'gray'
    }}
  >
    {e.answer}
  </Styles.Button>
)}
英文:

my problem is the following, when I go to check which alternative of the question is correct or incorrect, when I click on one of the buttons it works correctly with a question of giving the color of the situation of the item (if it is right or wrong), but it it's rendering color for all buttons, not just the one the user clicked.

I created a data file

export const Questions = [
  {
  alternatives: [
    { answer: &quot;OPT1&quot;, isCorrect: 1, alternative: &#39;A&#39; },
    { answer: &quot;OPT2&quot;, isCorrect: 2, alternative: &#39;B&#39; },
    { answer: &quot;OPT3&quot;, isCorrect: 2, alternative: &#39;C&#39; },
    { answer: &quot;OPT4&quot;, isCorrect: 2, alternative: &#39;D&#39; },
  ]
]

Error example: The first alternative is the correct one, when I click on it, the color of all buttons are changed to green, the same happens for when the alternative is incorrect (but for red color), I would like to change only the color of the clicked button, not all

This file I'm checking the situation of the question and giving the colors, in my file Question Modal. I first import the previous data file, and I'm creating the state and the function for the conditionals

const questions = Questions ?? [];
const [isCorrectQuestion, setIsCorrectQuestion] = useState&lt;number&gt;()
const test = (isCorrect: number) =&gt; {
  if (isCorrect === 1) {
   setIsCorrectQuestion(1)
  }
  else if (isCorrect === 2) {
   setIsCorrectQuestion(2)
  }
}

Finally, when rendering the alternatives, I'm mapping my data array and using the style property to check which alternative is right or wrong

{questions[0].alternatives.map(e =&gt;
 &lt;Styles.Button
  onClick={() =&gt; test(e.isCorrect)}
  style={
         {
          backgroundColor:
          isCorrectQuestion === 1 ? &#39;green&#39;
          : isCorrectQuestion === 2 ? &#39;red&#39;
          : &#39;gray&#39;
         }
        }
         &gt;
          {e.answer}
         &lt;/Styles.Button&gt;
  )}

I really can't say if I missed something unnoticed, or if it has some functionality or property that I don't know about and I'm not using it, but I wanted to know if following my logic I could make the button that is going to be clicked can only change the color.

答案1

得分: 1

而不是存储他们选择的替代方案是否正确,您可以存储所选替代方案的ID,然后根据每个单独替代方案的状态确定它应该是'绿色'还是'红色'

const questions = Questions ?? [];
const [selectedAlternative, setSelectedAlternative] = useState<string>(); // 'A' || 'B' || 'C' || 'D';

return (
  <div>
    {questions[0].alternatives.map((alt) => (
      <Styles.Button
        onClick={() => setSelectedAlternative(alt.alternative)}
        style={{
          backgroundColor:
            selectedAlternative === alt.alternative
              ? alt.isCorrect === 1
                ? 'green'
                : 'red'
              : 'gray'
        }}
      >
        {alt.answer}
      </Styles.Button>
    ))}
  </div>
);
英文:

Rather than storing whether the alternative they selected was correct, you could instead store the ID of the selected alternative and then determine whether it should be 'green' or 'red' based on the state of each individual alternative.

const questions = Questions ?? [];
const [selectedAlternative, setSelectedAlternative] = useState&lt;string&gt;(); // &#39;A&#39; || &#39;B&#39; || &#39;C&#39; || &#39;D&#39;

return (
  &lt;div&gt;
    {questions[0].alternatives.map((alt) =&gt; (
      &lt;Styles.Button
        onClick={() =&gt; setSelectedAlternative(alt.alternative)}
        style={{
          backgroundColor:
            selectedAlternative === alt.alternative
              ? alt.isCorrect === 1
                ? &#39;green&#39;
                : &#39;red&#39;
              : &#39;gray&#39;
        }}
      &gt;
        {alt.answer}
      &lt;/Styles.Button&gt;
    ))}
  &lt;/div&gt;
);

答案2

得分: 1

你正在使用一个状态来存储所有答案,这就是为什么所有答案都会受到影响。

我建议尝试以下解决方案:

  • 将点击的答案索引传递给测试函数并将其存储在状态中,将isCorrectQuestion更新为布尔值:
const [selectedAnswerIndex, setSelectedAnswerIndex] = useState<number>();
const [isCorrectQuestion, setIsCorrectQuestion] = useState<boolean>();
const test = (isCorrect: number, answerIndex: number) => {
  setSelectedAnswerIndex(answerIndex);
  if (isCorrect === 1) {
    setIsCorrectQuestion(true);
  } else if (isCorrect === 2) {
    setIsCorrectQuestion(false);
  }
}

然后更新你的JSX如下:

{questions[0].alternatives.map((e, i) =>
  <Styles.Button
    onClick={() => test(e.isCorrect, i)}
    style={
      {
        backgroundColor:
          isCorrectQuestion && selectedAnswerIndex === i ? 'green'
          : !isCorrectQuestion && selectedAnswerIndex === i ? 'red'
          : 'gray'
      }
    }
  >
    {e.answer}
  </Styles.Button>
)}
英文:

You are using a single state for all of the answers, that's why all answers are affected.

I would suggest trying out the following solution:

  • pass clicked answer index to the test function and store it in a state, update isCorrectQuestion to be a Boolean:
const [selectedAnswerIndex, setSelectedAnswerIndex] = useState&lt;number&gt;();
const [isCorrectQuestion, setIsCorrectQuestion] = useState&lt;boolean&gt;()
const test = (isCorrect: number, answerIndex: number) =&gt; {
  setSelectedAnswerIndex(answerIndex);
  if (isCorrect === 1) {
   setIsCorrectQuestion(true);
  }
  else if (isCorrect === 2) {
   setIsCorrectQuestion(false);
  }
}

Then update your JSX as follows:

{questions[0].alternatives.map((e, i) =&gt;
 &lt;Styles.Button
  onClick={() =&gt; test(e.isCorrect, i)}
  style={
         {
          backgroundColor:
          isCorrectQuestion &amp;&amp; selectedAnswerIndex === i ? &#39;green&#39;
          : !isCorrectQuestion &amp;&amp; selectedAnswerIndex === i ? &#39;red&#39;
          : &#39;gray&#39;
         }
        }
         &gt;
          {e.answer}
         &lt;/Styles.Button&gt;
  )}

huangapple
  • 本文由 发表于 2023年2月8日 23:44:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定