React数字验证并在提交数字输入后更新状态,而不是在onChange时更新。

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

React number validation and update state after submitting number input, not onChange

问题

我在通过数字输入更新状态时遇到了一些检查问题。我正在使用React Three Fiber构建一个3D配置器,希望通过侧边栏使房间大小可变。全局状态在zustand存储中更新正常,但我在希望在值离开输入字段后运行的检查方面遇到了问题。我已经研究过onSubmit和onBlur,但似乎它们不起作用...

谢谢你们!

英文:

I had some issues with some checks when I update number values in a state through a number input.

I'm working on a 3Dconfigurator in React Three Fiber and want to make my room size variable through my sidepanel. Global states are updated in a zustand store and that works fine, but I keep having issues with the checks I want to run on my values. I can only get them to run on change, which prevents me from updating the numbers properly.

How can I update the state and run the checks only after leaving the input fields?
I already researched onSubmit and onBlur but those seemed not to work...

Thank you all in advance!

  1. import { useAppStore } from "@/stores/appStore";
  2. const Roomdimensions = () => {
  3. const update = useAppStore((state) => state.update);
  4. const room = useAppStore((state) => state.room);
  5. const changeRoom = () => {
  6. let roomwidth = document.querySelector("#width").value;
  7. let roomheight = document.querySelector("#height").value;
  8. let roomdepth = document.querySelector("#depth").value;
  9. if (roomwidth < 100 || roomwidth > 1200) {
  10. update({
  11. room: { width: 300, height: roomheight, depth: roomdepth },
  12. showAlert: true,
  13. alertAction: "notification",
  14. alertMessage: "Room width must be between 100cm and 1200cm",
  15. });
  16. }
  17. if (roomdepth < 100 || roomdepth > 1200) {
  18. roomdepth = 300;
  19. update({
  20. room: { width: roomwidth, height: roomheight, depth: 300 },
  21. showAlert: true,
  22. alertAction: "notification",
  23. alertMessage: "Room depth must be between 100cm and 1200cm",
  24. });
  25. }
  26. if (roomheight < 180 || roomheight > 300) {
  27. roomheight = 210;
  28. update({
  29. room: { width: roomwidth, height: 210, depth: roomdepth },
  30. showAlert: true,
  31. alertAction: "notification",
  32. alertMessage: "Room height must be between 180cm and 300cm",
  33. });
  34. }
  35. const room = {
  36. width: roomwidth,
  37. height: roomheight,
  38. depth: roomdepth,
  39. };
  40. update({ room });
  41. };
  42. return (
  43. <div className="room">
  44. <div className="room__input">
  45. <span className="room__input__container">
  46. <label htmlFor="room--width">Width:</label>
  47. <input
  48. type="number"
  49. name="room--width"
  50. id="width"
  51. value={room.width}
  52. onChange={changeRoom}
  53. />
  54. <div className="room__input__unit">cm</div>
  55. </span>
  56. <small className="room__input__limits">min. 100cm / max. 1200cm</small>
  57. </div>
  58. <div className="room__input">
  59. <span className="room__input__container">
  60. <label htmlFor="room--height">Height:</label>
  61. <input
  62. type="number"
  63. name="room--height"
  64. id="height"
  65. value={room.height}
  66. onChange={changeRoom}
  67. />
  68. <div className="room__input__unit">cm</div>
  69. </span>
  70. <small className="room__input__limits">min. 160cm / max. 300cm</small>
  71. </div>
  72. <div className="room__input">
  73. <span className="room__input__container">
  74. <label htmlFor="room--depth">Depth:</label>
  75. <input
  76. type="number"
  77. name="room--depth"
  78. id="depth"
  79. value={room.depth}
  80. onChange={changeRoom}
  81. />
  82. <div className="room__input__unit">cm</div>
  83. </span>
  84. <small className="room__input__limits">min. 100cm / max. 1200cm</small>
  85. </div>
  86. </div>
  87. );
  88. };
  89. export default Roomdimensions;

答案1

得分: 1

onBlur 在更新三个维度时有效,但不适用于同时更新所有三个维度。

感谢 @LynnC.,让我查看 onfocusout 并重新开始研究这个错误。

英文:

onBlur did the trick just fine, just not while updating all three of the dimensions at once.

Thank you @LynnC. to make me check out onfocusout and get back on track with researching the bug.

huangapple
  • 本文由 发表于 2023年4月17日 22:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036042.html
匿名

发表评论

匿名网友

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

确定