Skip to content

Instantly share code, notes, and snippets.

@andrepcg
Created October 26, 2018 23:12
Show Gist options
  • Save andrepcg/8aac734765714c85ff19a2a58e05cfd8 to your computer and use it in GitHub Desktop.
Save andrepcg/8aac734765714c85ff19a2a58e05cfd8 to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
const useCounter = (initialValue) => {
const [count, setCount] = useState(initialValue);
const increment = () => setCount(count + 1);
const decrement = () => setCount(count - 1);
return { count, increment, decrement };
};
function Counter() {
const { count, increment, decrement } = useCounter(0);
return (
<div>
<p>{count}</p>
<button onClick={increment}>
increment
</button>
<button onClick={decrement}>
decrement
</button>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment