为什么要获取指定值“Result”?无法解析,或者超出范围?

发布于 2025-01-13 03:33:00 字数 1791 浏览 2 评论 0原文

我有一个临界角计算器作为 ReactJs 组件。但我在单击“计算”按钮时收到此警告:无法解析指定的值“Result”或超出范围。有人能指出错误吗?下面提到了组件的代码:

 const CriticalAngle = () => {
    const [n1, setN1] = useState(null);
    const [n2, setN2] = useState(null);
    const [result, setResult] = useState(null);
    const calcResult=()=>{
      setResult(57.29578*Math.asin(n2/n1));
    }
    
    return (
      <>
        <Form>
          <Form.Group className="mb-4">
            <Form.Label>Refractive Index of Incident Medium(Denser Medium)</Form.Label>
            <Form.Control
              type="number"
              placeholder="Enter the value of n1"
              onChange={(e) => setN1(Number(e.target.value))}
              value={n1 === null ? "" : n1}
            />
          </Form.Group>
          <Form.Group className="mb-4">
            <Form.Label>Refractive Index of Refractive Medium(Rarer Medium)</Form.Label>
            <Form.Control
              type="number"
              placeholder="Enter the value of n2"
              onChange={(e) => setN2(Number(e.target.value))}
              value={n2 === null ? "" : n2}
            />
          </Form.Group>
          <Form.Group className="mb-4">
            <Form.Label>Critical Angle</Form.Label>
            <Form.Control
              type="number"
              disabled="true"
              value={result === null ? "Result" : result}
            />
          </Form.Group>
        </Form>
        <div className="button-custom-grp">
          <Button variant="primary" onClick={calcResult}>
            Calculate
          </Button>

       
        </div>
      </>
    )
  }


I have a Critical Angle calculator as a ReactJs Component. But I am getting this warning upon clicking on the Calculate button: The specified value "Result" cannot be parsed or is out of range. Can someone point out the error?The code for the Component is mentioned below:

 const CriticalAngle = () => {
    const [n1, setN1] = useState(null);
    const [n2, setN2] = useState(null);
    const [result, setResult] = useState(null);
    const calcResult=()=>{
      setResult(57.29578*Math.asin(n2/n1));
    }
    
    return (
      <>
        <Form>
          <Form.Group className="mb-4">
            <Form.Label>Refractive Index of Incident Medium(Denser Medium)</Form.Label>
            <Form.Control
              type="number"
              placeholder="Enter the value of n1"
              onChange={(e) => setN1(Number(e.target.value))}
              value={n1 === null ? "" : n1}
            />
          </Form.Group>
          <Form.Group className="mb-4">
            <Form.Label>Refractive Index of Refractive Medium(Rarer Medium)</Form.Label>
            <Form.Control
              type="number"
              placeholder="Enter the value of n2"
              onChange={(e) => setN2(Number(e.target.value))}
              value={n2 === null ? "" : n2}
            />
          </Form.Group>
          <Form.Group className="mb-4">
            <Form.Label>Critical Angle</Form.Label>
            <Form.Control
              type="number"
              disabled="true"
              value={result === null ? "Result" : result}
            />
          </Form.Group>
        </Form>
        <div className="button-custom-grp">
          <Button variant="primary" onClick={calcResult}>
            Calculate
          </Button>

       
        </div>
      </>
    )
  }


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏蝶舞 2025-01-20 03:33:00

好吧,我不知道这些标签是什么,但这是我的猜测...

临界角的控制被标记为 type="number",因此它不喜欢不符合的值数字,就像字符串“Result”一样。

相反,你可以尝试这样的事情。我使用 Form.Label 标记来包装“Result”字符串,但我认为还有更好的方法。

      <Form.Group className="mb-4">
        <Form.Label>Critical Angle</Form.Label>
        {result === null
           ? (
             <Form.Label>Result</Form.Label>
           ) : (
             <Form.Control
                type="number"
                disabled="true"
                value={result === null ? "Result" : result}
             />
           )
        }
      </Form.Group>

我无法提供更多帮助。

Well, I don't know what those tags are, but here is my guess...

The critical angle's control is marked as type="number", hence it doesn't like values which are not numeric, like the string "Result" is.

Instead, you could try something like this. I used a Form.Label tag to wrap the "Result" string, but I think there's something better.

      <Form.Group className="mb-4">
        <Form.Label>Critical Angle</Form.Label>
        {result === null
           ? (
             <Form.Label>Result</Form.Label>
           ) : (
             <Form.Control
                type="number"
                disabled="true"
                value={result === null ? "Result" : result}
             />
           )
        }
      </Form.Group>

I can't help more than this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文