React custom hook for form state management
<em>Goal:</em> defining a custom useForm hook for managing the state of our forms. This can have some initial state, mainly used for precompleting update forms, but can also provide empty form fields.
Problem: when we want to operate on a precompleted form, altough we pass all the data for our fields, the form is not being precompleted and its fields are empty.
```
export default function useForm(initial = {}) {
const [inputs, setInputs] = useState(initial);
const handleChange = (e) => {
let { value, name, type } = e.target;
if (type === 'number') {
value = parseInt(value);
}
if (type === 'file') {
[value] = e.target.files;
}
setInputs({
...inputs,
[name]: value,
});
};
const resetForm = () => {
setInputs(initial);
};
const clearForm = () => {
const blankState = Object.fromEntries(
Object.entries(inputs).map(([key, value]) => [key, ''])
);
setInputs(blankState);
};
return {
inputs,
clearForm,
resetForm,
handleChange,
};
}
```
This is what happens:
1. We pass an initial state object which is undefined (server-side rendered) until the GQL query loads.
2. This initial object (which is undefined) populates the form fields making them empty.
3. After the query loads, the initial state object is repassed to the useForm hook, but the DOM is not rerendered => a possible solution is to make use of the useEffect() hook for forcing rerendering.
4. We cannot watch for changes directly on the initial object and reassign it using setInputs, because it triggers the useEffect callback once again and again and again when altering its value, causing an infinite loop.
5. The solution is to watch for changes on a string joined by the values of the initial object. When that changes from undefined to the GraphQL query results, the useEffect callback is called and it initializes and rerenders the fields correspondingly.
An example implementation could be:
```js
const initialValues = Object.values(initial).join('');
useEffect(() => {
setInputs(initial);
}, [initialValues]);
```
Now the form precompletion works fine using our custom useForm() hook.
Rares Raulea
22 Mar 2022
« Back to post