Created
March 21, 2022 18:39
-
-
Save asaumet230/fc6bdc260897f97136357596ac076b27 to your computer and use it in GitHub Desktop.
Custom Hook UseCounter
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
import { useState } from "react"; | |
const useCounter = ( initialState = 1 ) => { | |
const [ counter, setCounter ] = useState( initialState ); | |
const increment = () => { | |
setCounter( counter + 1 ); | |
} | |
const decrement = () => { | |
setCounter( counter - 1 ); | |
} | |
const reset = ()=> { | |
setCounter( initialState ); | |
} | |
return { | |
counter, | |
increment, | |
decrement, | |
reset | |
} | |
} | |
export default useCounter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment