Skip to content

Instantly share code, notes, and snippets.

@isacjunior
Created March 27, 2019 20:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isacjunior/66a79a0f3ac3667d1f8c97049d3390a3 to your computer and use it in GitHub Desktop.
Save isacjunior/66a79a0f3ac3667d1f8c97049d3390a3 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react'
interface Props {
initialState: number,
step: number
}
function useCounter({ initialState, step }: Props) {
const [count, setCount] = useState(initialState)
const increment = () => setCount(count + step)
return { count, increment }
}
function Counter() {
const { count, increment } = useCounter({ initialState: 0, step: 1 })
return <button onClick={increment}>{count}</button>
}
export default Counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment