取消选中 React 中的单选按钮在 useEffect 后不起作用。

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

Unchecking Radio buttons is not working with React after useeffect

问题

以下是您表单中的翻译部分:

<Form>
  <ListGroup>
    <ListGroup.Item className="text-start">
      <div>
        <Form.Check
          type="radio"
          id="0"
          name="answer"
          label={questionsJSON.questions[num].answers[0]}
          value={questionsJSON.questions[num].answers[0]}
          onChange={(e) => setSelectedAnswer(e.target.value)}
          {...selectedAnswer != null ? "checked" : null}
        />
      </div>
    </ListGroup.Item>
    <ListGroup.Item className="text-start">
      <Form.Check
        type="radio"
        id="1"
        name="answer"
        label={questionsJSON.questions[num].answers[1]}
        value={questionsJSON.questions[num].answers[1]}
        onChange={(e) => setSelectedAnswer(e.target.value)}
      />
    </ListGroup.Item>
    <ListGroup.Item className="text-start">
      <Form.Check
        type="radio"
        id="2"
        name="answer"
        label={questionsJSON.questions[num].answers[2]}
        value={questionsJSON.questions[num].answers[2]}
        onChange={(e) => setSelectedAnswer(e.target.value)}
        {...selectedAnswer != null ? "checked" : null}
      />
    </ListGroup.Item>
    <ListGroup.Item className="text-start">
      <Form.Check
        type="radio"
        id="3"
        name="answer"
        label={questionsJSON.questions[num].answers[3]}
        value={questionsJSON.questions[num].answers[0]}
        onChange={(e) => setSelectedAnswer(e.target.value)}
        {...selectedAnswer != null ? "checked" : null}
      />
    </ListGroup.Item>
  </ListGroup>
  <br />
  <Button variant="danger" onClick={onClickPassRadio}>
    submit
  </Button>
</Form>

有关您的问题,首次提交后,您希望将所有单选按钮取消选择,但之后在 useEffect 中将 selectedAnswer 设置为 null 时,界面上显示为选中状态,尽管实际上它不应该选中。要解决此问题,您可以尝试以下更改:

const onClickPassRadio = (e) => {
  e.preventDefault();
  setSelectedAnswer("");
  setNum(randomNumberInRange(10));
};

通过在 onClickPassRadio 函数中将 setSelectedAnswer 设置为空字符串,您可以确保提交后所有单选按钮都取消选择。这样,即使在 useEffect 中将 selectedAnswer 设置为 null,界面上也不会显示选中状态。

英文:

Below is my Form looks like in return section:

&lt;Form &gt;
&lt;ListGroup &gt;
&lt;ListGroup.Item className=&quot;text-start&quot; &gt;
&lt;div&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;0&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[0]}
value={questionsJSON.questions[num].answers[0]}
onChange={e =&gt; setSelectedAnswer(e.target.value)}
{...{ selectedAnswer } != null ? &quot;checked&quot; : null}
/&gt;
&lt;/div&gt;
&lt;/ListGroup.Item&gt;
&lt;ListGroup.Item className=&quot;text-start&quot;&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;1&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[1]}
value={questionsJSON.questions[num].answers[1]}
onChange={e =&gt; setSelectedAnswer(e.target.value)}
/&gt;
&lt;/ListGroup.Item&gt;
&lt;ListGroup.Item className=&quot;text-start&quot;&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;2&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[2]}
value={questionsJSON.questions[num].answers[2]}
onChange={e =&gt; setSelectedAnswer(e.target.value)}
{...{ selectedAnswer } != null ? &quot;checked&quot; : null}
/&gt;
&lt;/ListGroup.Item&gt;
&lt;ListGroup.Item className=&quot;text-start&quot;&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;3&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[3]}
value={questionsJSON.questions[num].answers[0]}
onChange={e =&gt; setSelectedAnswer(e.target.value)}
{...{ selectedAnswer } != null ? &quot;checked&quot; : null}
/&gt;
&lt;/ListGroup.Item&gt;
&lt;/ListGroup&gt;
&lt;br /&gt;
&lt;Button variant=&quot;danger&quot; onClick={onClickPassRadio}&gt;submit&lt;/Button&gt;

</Form>

What is happening is first time it works ie all the radio buttons are unchecked once Submit is pressed. But afterwards, useeffect is called and selectedAnswer is being set to null too. But even if I dont click any option, on UI it is shown as checked when it is not as useeffect afterwards shows value as null.

Is there a way to uncheck all the radio buttons once submit is pressed?

    const [selectedAnswer, setSelectedAnswer] = useState(&quot;&quot;);
const [num, setNum] = useState(0);
const randomNumberInRange = ( max) =&gt; {
return Math.floor(Math.random() 
* (max  + 1));
};
const onClickPassRadio = (e) =&gt; {
e.preventDefault();
//console.log(selectedAnswer);
setNum(randomNumberInRange(10));
};
useEffect(() =&gt; {
setSelectedAnswer(null);
}, [setNum]);

答案1

得分: 1

<Form>
  <ListGroup>
    <ListGroup.Item className="text-start">
      <div>
        <Form.Check
          type="radio"
          id="0"
          name="answer"
          label={questionsJSON.questions[num].answers[0]}
          value={questionsJSON.questions[num].answers[0]}
          onChange={(e) => setSelectedAnswer(e.target.value)}
          checked={selectedAnswer === questionsJSON.questions[num].answers[0]}
        />
      </div>
    </ListGroup.Item>
    <ListGroup.Item className="text-start">
      <Form.Check
        type="radio"
        id="1"
        name="answer"
        label={questionsJSON.questions[num].answers[1]}
        value={questionsJSON.questions[num].answers[1]}
        onChange={(e) => setSelectedAnswer(e.target.value)}
        checked={selectedAnswer === questionsJSON.questions[num].answers[1]}
      />
    </ListGroup.Item>
    <ListGroup.Item className="text-start">
      <Form.Check
        type="radio"
        id="2"
        name="answer"
        label={questionsJSON.questions[num].answers[2]}
        value={questionsJSON.questions[num].answers[2]}
        onChange={(e) => setSelectedAnswer(e.target.value)}
        checked={selectedAnswer === questionsJSON.questions[num].answers[2]}
      />
    </ListGroup.Item>
    <ListGroup.Item className="text-start">
      <Form.Check
        type="radio"
        id="3"
        name="answer"
        label={questionsJSON.questions[num].answers[3]}
        value={questionsJSON.questions[num].answers[3]}
        onChange={(e) => setSelectedAnswer(e.target.value)}
        checked={selectedAnswer === questionsJSON.questions[num].answers[3]}
      />
    </ListGroup.Item>
  </ListGroup>
  <br />
  <Button variant="danger" onClick={onClickPassRadio}>
    submit
  </Button>
</Form>
英文:
&lt;Form&gt;
&lt;ListGroup&gt;
&lt;ListGroup.Item className=&quot;text-start&quot;&gt;
&lt;div&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;0&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[0]}
value={questionsJSON.questions[num].answers[0]}
onChange={(e) =&gt; setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[0]}
/&gt;
&lt;/div&gt;
&lt;/ListGroup.Item&gt;
&lt;ListGroup.Item className=&quot;text-start&quot;&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;1&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[1]}
value={questionsJSON.questions[num].answers[1]}
onChange={(e) =&gt; setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[1]}
/&gt;
&lt;/ListGroup.Item&gt;
&lt;ListGroup.Item className=&quot;text-start&quot;&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;2&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[2]}
value={questionsJSON.questions[num].answers[2]}
onChange={(e) =&gt; setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[2]}
/&gt;
&lt;/ListGroup.Item&gt;
&lt;ListGroup.Item className=&quot;text-start&quot;&gt;
&lt;Form.Check
type=&quot;radio&quot;
id=&quot;3&quot;
name=&quot;answer&quot;
label={questionsJSON.questions[num].answers[3]}
value={questionsJSON.questions[num].answers[0]}
onChange={(e) =&gt; setSelectedAnswer(e.target.value)}
checked={selectedAnswer === questionsJSON.questions[num].answers[3]}
/&gt;
&lt;/ListGroup.Item&gt;
&lt;/ListGroup&gt;
&lt;br /&gt;
&lt;Button variant=&quot;danger&quot; onClick={onClickPassRadio}&gt;
submit
&lt;/Button&gt;
&lt;/Form&gt;

答案2

得分: 0

为了在按下提交按钮后取消选中所有单选按钮,您需要将selectedAnswer状态更新为一个空字符串(""),而不是null。以下是应该实现所需行为的修改后的代码:

useEffect(() => {
    setSelectedAnswer(""); // 将其更新为空字符串而不是null
}, [num]);
英文:

To uncheck all the radio buttons once the submit button is pressed, you need to update the selectedAnswer state to an empty string ("") instead of null. Here's the modified code that should achieve the desired behavior:

useEffect(() =&gt; {
setSelectedAnswer(&quot;&quot;); // Update to an empty string instead of null
}, [num]);

答案3

得分: 0

希望这个对你有所帮助。

const onClickPassRadio = (e) => {
    e.preventDefault();
    setSelectedAnswer(""); // 重置selectedAnswer以取消选中所有单选按钮
    setNum(randomNumberInRange(10));
};
英文:

Hope this one help you.

const onClickPassRadio = (e) =&gt; {
e.preventDefault();
setSelectedAnswer(&quot;&quot;); // reset selectedAnswer to uncheck all radio buttons
setNum(randomNumberInRange(10));
};

huangapple
  • 本文由 发表于 2023年7月17日 20:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704647.html
匿名

发表评论

匿名网友

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

确定