Q:

TypeError: Cannot read property 'setValues' of undefined | React

when i use setValues() function on handleInputChange i get this error:

code:
import React, { useState, useEffect } from "react";

const useForm = (initialFieldValues) => {
  const { values, setValues } = useState(initialFieldValues);

  const handleInputChange = (e) => {
    const { name, value } = e.target;
    const fieldValue = { [name]: value };

    this.setValues({
      ...values,
      ...fieldValue,
    });
  };

  return {
    values,
    setValues,
    handleInputChange,
  };
};

export default useForm;

how can i solve this error ?
any response will be appreciated

A:

import React, { useState, useEffect } from "react";

const useForm = (initialFieldValues) => {
  const [ values, setValues ] = useState(initialFieldValues);

  const handleInputChange = (e) => {
    const { name, value } = e.target;
    const fieldValue = { [name]: value };

    setValues({
      ...values,
      ...fieldValue,
    });
  };

  return {
    values,
    setValues,
    handleInputChange,
  };
};

export default useForm;

