clearing the value of an input field in react.js
to clear the value of an input field in react:
- store the input value in a state variable.
- when a certain event occurs, set the state variable to an empty string.
-
for uncontrolled components, set the value of ref to an empty string,
ref.current.value = '';
e.g.
import {usestate} from 'react';
const app = () => {
// 👇️ 将输入的值存储在状态中
const [message, setmessage] = usestate('');
const handlechange = event => {
setmessage(event.target.value);
};
const handleclick = () => {
// 👇️ 清除 input 值
setmessage('');
};
return (
<div><inputid="message"name="message"type="text"onchange={handlechange}value={message}
/><buttononclick={handleclick}>clear fieldbutton>div>
);
};
exportdefaultapp;
we use usestate
the hook to store the value of the input field in a state variable.
to clear the entered value, we have to set the message status variable to an empty string.
every time the button is clicked, the function we passed to onclick
the attribute of the button element will be called handleclick
.
the button can be a submit button for a form, or a simple button that triggers an event.
this method can be used to reset the values of as many input fields as needed.
useref
if we use an uncontrolled component
via the hook, set the value of ref to an empty string.
import {useref} from 'react';
const app = () => {
const ref = useref(null);
const handleclick = () => {
// 👇️ 清除 input 字段值
ref.current.value = '';
};
return (
<div><inputref={ref}id="message"name="message"type="text" /><buttononclick={handleclick}>clear fieldbutton>div>
);
};
this code example achieves the same result, but for an uncontrolled component.
useref()
the hook can be passed an initial value as an argument. the hook returns a mutable ref object whose .current
attributes are initialized to the passed argument.
注意
, we have to access the current property of the ref object to access the input element on which we set the ref attribute.
when we pass a ref prop to an element, for example , react
.current
sets the property of the ref object to the corresponding dom node.
this method can be used to clear the values of as many uncontrolled input fields as needed.
alternatively, when using uncontrolled input fields, we can also use reset()
the method to clear the values of all input fields in the form.
import {useref} from 'react';
const app = () => {
const firstref = useref(null);
const lastref = useref(null);
const handlesubmit = event => {
console.log('handlesubmit ran');
event.preventdefault();
// 👇️ 清除表单中的所有输入值
event.target.reset();
};
return (
<div>
<form onsubmit={handlesubmit}>
<input
ref={firstref}
id="first_name"
name="first_name"
type="text"
/>
<input
ref={lastref}
id="last_name"
name="last_name"
type="text"
/>
<button type="submit">submit formbutton>
form>
div>
);
};
export default app;
注意
, this approach does not work for controlled components where we store the value of the input field in the state.
we handlesubmit
used event.preventdefault()
a method in the function to prevent the page from refreshing when the form is submitted.
reset()
method restores the default values of form elements.
no matter how many uncontrolled input fields our form has, reset()
a single call to the method will clear all of them.
for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.
article url:
related articles
deleting keys from state object in react
publish date:2025/03/10 views:62 category:react
-
要从 react 状态对象中删除键: 使用 usestate 挂钩来存储状态对象。 解构对象的键和其余属性。 将状态设置为其余属性。
publish date:2025/03/09 views:170 category:react
-
要在 react 状态下替换数组中的对象:使用 map() 方法遍历数组。 在每次迭代中,检查是否满足某个条件。 替换满足条件的对象并按原样返回所有其他对象。
how to call a function only once in react
publish date:2025/03/09 views:127 category:react
-
使用 useeffect 钩子在 react 中只调用一次函数。 当 useeffect 钩子传递一个空的依赖数组时,它仅在组件挂载时运行。 当我们必须在组件安装时获取数据时,这是首选方法。
run react hooks when a component unmounts
publish date:2025/03/09 views:81 category:react
-
卸载组件时,使用 useeffect 钩子运行react钩子。 我们从 useeffect 钩子返回的函数在组件卸载时被调用,可用于清理目的。
publish date:2025/03/09 views:181 category:react
-
在 react 中使用带有索引的 map() 方法:在数组上调用 map() 方法。传递给 map() 方法的函数会被元素和索引调用。
setting onclick listener on links in react
publish date:2025/03/09 views:182 category:react
-
要在 react 中的链接上设置 onclick 监听器:在链接上设置 onclick 属性。每次单击链接时,都会调用我们传递给道具的函数。
remove an object the state array in react
publish date:2025/03/09 views:72 category:react
-
在 react 中从状态数组中移除一个对象:使用 filter() 方法遍历数组 在每次迭代中,检查是否满足条件。将状态设置为 filter 方法返回的新数组。
how to create a sleep function in react.js
publish date:2025/03/09 views:191 category:react
-
在 react 中创建 sleep 函数:定义一个以毫秒数为参数的函数。该函数应返回一个 promise,该 promise 在提供的毫秒数后得到解决。
how to handle visibility: hidden in react
publish date:2025/03/09 views:62 category:react
-
在 react 中将 css visibility 属性设置为 hidden:在指示元素是否应该可见的状态中存储一个布尔值。有条件地在元素的样式属性中设置可见性属性。例如,style={{visibility: isvisible ? “visible”