Referenced to resetting a form with baseState
in React component.
I am using Hooks (useState
etc.) in my React component that returns a form. Is there any way to use this.baseState
to reset the input values when we use Hooks?
PS.
Please check the code snippet below. I want to reset the form (set base state) when the user clicks on the Cancel button.
CODE:
import React, { useState } from 'react';
import { render } from 'react-dom';
import PropTypes from 'prop-types';
const TasksTemplateEditForm = ({
handleFormResetOnCancel,
handleFormSubmit
}) => {
const [name, setName] = useState("default value");
const [description, setDescription] = useState("default value");
const [is_general, setIsGeneral] = useState("default value");
return (
<form>
<div>
<label>Template Name</label>
<input
type="text"
name="name"
onChange={e => setName(e.target.value)}
value={name}
/>
</div>
<div className="form-group">
<label>Description</label>
<textarea
name="description"
onChange={e => setDescription(e.target.value)}
value={description}
></textarea>
</div>
<button onClick={() => handleFormResetOnCancel()} >CANCEL</button>
<button onClick={() => handleFormSubmit()} >UPDATE CHANGES</button>
</form>
);
};