Skip to content

Instantly share code, notes, and snippets.

@asaumet230
Created March 21, 2022 18:39
Show Gist options
  • Save asaumet230/fc6bdc260897f97136357596ac076b27 to your computer and use it in GitHub Desktop.
Save asaumet230/fc6bdc260897f97136357596ac076b27 to your computer and use it in GitHub Desktop.
Custom Hook UseCounter
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