解决方案
export const blockInvalidChar = e => ['e', 'E', '+', '-'].includes(e.key) && e.preventDefault();
解决方案非常简单。我们将编写一个可重用的 util 函数,它基本上会阻止无效字符。
import React, { useState } from "react";
import { blockInvalidChar } from "./blockInvalidChar";
import "./styles.css";
export default function App() {
const [value, setValue] = useState("");
return (
<div className="App">
<input
type="Number"
value={value}
onKeyDown={blockInvalidChar}
onChange={({ target: { value } }) => {
setValue(value);
}}
/>
</div>
);
}
只是简单记录一下,解决方案。以备下次查找。以下是原文地址 原文地址 dev.to/narendersai…