Last active
February 13, 2023 18:26
-
-
Save prodriguezr/a463f53d66610f36d6b3f1e46294238d to your computer and use it in GitHub Desktop.
Custom Hook for reactjs javascript project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* How to implement?: | |
| const initialForm = { | |
| name: '', | |
| email: '', | |
| age: 0, | |
| } | |
| const [ formValues, handleInputChange, reset ] = useForm(initialForm); | |
| */ | |
| import { useState } from 'react'; | |
| export const useForm = (initialState = {}) => { | |
| const [values, setValues] = useState(initialState); | |
| const reset = () => { | |
| setValues(initialState); | |
| }; | |
| const handleInputChange = ({ target: { name, value } }) => { | |
| console.log(`handleInputChange: ${value}`); | |
| setValues({ | |
| ...values, | |
| [name]: value, | |
| }); | |
| }; | |
| return [values, handleInputChange, reset]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment