Create a Custom React Hook to Handle Form Fields

Link to chapter -

Question-- How would you go about clearing all of the fields in that custom hook? For example, you could have a form where a user could want to submit multiple times; how could you use this hook to easily set each field to an empty string?

That’s a good question. For that I would return another function with the hook that resets the state to the initialState.

I don’t understand the line in the custom React hook:


Why assign to

The explanation is:

The only difference here is that we are using (which contains the id of our form field) to store the value ( ).

I still don’t get it.

Ah, I added a note and edited the chapter. Hope that helps.

In the case of our form the elements, the is set as the the controlId. So we are using that as the id in our state.

<FormGroup controlId="email" bsSize="large">
   onChange={e => setEmail(}
1 Like

Got it. So in the state object we end up with a property called “email” that has the value of what the user entered in the form. Thanks!

MS Edge still doesn’t support the es6 spread operator. Therefore this hook will cause the following error: SCRIPT1028 “Expected identifier, string or number”

Whoa that’s weird. Can somebody else confirm this?

How about add react-app-polyfill?

How do you reset the values, let’s say after form submit?

It would take a bit more code. But it might work something like this (haven’t quite tested this yet):

export function useFormFields(initialState) {
  const [fields, setValues] = useState(initialState);

  return [
    function(event) {
    function () {
      setValues({ ...fields });

Here the third parameter returned is a reset function. If it’s called, it would reset the fields. However, if you are going to have more than 2 returned values in an array, it would be more descriptive to return it as an object.

I might be having a slow day but it took me a while to understand how this hook works.
I think a comment like this could help:

“useState can set multiple values at once when passed an array, or it can set a single value when passed just one value. The setValues function returned by useState can update multiple values or just a single value. We pass a single key:value pair when used with the onChange event.”

1 Like

Thanks for this! I update the chapter with a better description of how this hook works.

1 Like

Cool thanks, hope that helps someone else out there too.

1 Like