Skip to content

Instantly share code, notes, and snippets.

@matheusml
Last active February 11, 2021 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matheusml/a5e8f16ccf00ecc0e2ce9b09628a3f53 to your computer and use it in GitHub Desktop.
Save matheusml/a5e8f16ccf00ecc0e2ce9b09628a3f53 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
export function useCounter() {
const [count, setCount] = useState(0);
const decrement = () => setCount(count - 1);
const increment = () => setCount(count + 1);
return {
count,
decrement,
increment,
};
}
export function Increment() {
const counter = useCounter();
return (
<Fragment>
<div>Count: {counter.count}</div>
<div>
<button onClick={counter.increment}>Increment</button>
</div>
</Fragment>
);
}
export function Decrement() {
const counter = useCounter();
return (
<Fragment>
<div>Count: {counter.count}</div>
<div>
<button onClick={counter.decrement}>Decrement</button>
</div>
</Fragment>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment