Reset form input values in React
You need to make your inputs controlled by passing the value you store in your state
then you just have to reset the state values and your component value resets.
check this sample below
handleInputChange = (e) => {
let { name, value } = e.target;
this.setState({
...this.state,
inputs: {
[name]: value
}
});
}
your component will now look like
<input name='fullName' value={this.state.inputs.fullName} onChange={this.handleInputChange} />
Your reset function will just clear the state and your input field will be empty since it's controlled via state
resetInputFields = () => {
this.setState({ inputs: {} })
}
How to reset input field from useRef in React?
You can clear the text input field by setting its value to an empty string. You can do that like this inputref.current.value = ""
if you want to use uncontrolled inputs.
However, if you want to use controlled inputs you can create a state variable to track the value of the input field. For example,
const SomeComponent = () => {
const [text, setText] = useState('')
return (
<>
<input type="text" value={text} onChange={(e) => setText(e.target.value)} />
<button onClick={() => setText('')}>delete all</button>
</>
);
};
Here is a codesandbox with both implementation.
How reset form values using react bootstrap
I don't commonly use uncontrolled components, but I think you could solve this by adding setValidated(false)
and event.target.reset()
to the handleClickTransitionAlert
, like this:
const handleClickTransitionAlert = (event) => {
setTransitionAlert(true);
setTimeout(() => {
setTransitionAlert(false);
setValidated(false)
event.target.reset()
}, 1700);
};
Related Topics
How to Hash Passwords Before Posting and Then Using Bcrypt
Formdata Created from an Existing Form Seems Empty When I Log It
How to Save an Image to Localstorage and Display It on the Next Page
How to Disable Button After (Click) in Angular
How to Reload Datatable by Ajax (Jquery)
Merge Array of Objects by Same Key-Value
Hide Elements If User Logged In
Convert Jquery Code to JavaScript
Cannot Open Local File - Chrome: Not Allowed to Load Local Resource
Javascript to Check for Event Without Page Refresh
Javascript Generate Video Thumbnail from Video Url
Calculate Percentage JavaScript
Download Image as File in Typescript
Javascript Generate Random Numbers Without Repeating
How to Prevent Invalid Characters from Being Typed into Input Fields
Get Part of the Url Pathname Via JavaScript Regex