Skip to content

Instantly share code, notes, and snippets.

@jackyef
Last active October 28, 2018 08:37
Show Gist options
  • Save jackyef/e40b27331eca14207f15250af6c0b221 to your computer and use it in GitHub Desktop.
Save jackyef/e40b27331eca14207f15250af6c0b221 to your computer and use it in GitHub Desktop.
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
const useCounter = () => {
const [count, setCount] = useState(0);
const onIncrease = () => setCount(count + 1);
const onDecrease = () => setCount(count - 1);
return [ count, onIncrease, onDecrease ];
};
const App = () => {
const [ count, onIncrease, onDecrease ] = useCounter();
return (
<div>
<div>Current count: {count}</div>
<div>
<button onClick={onDecrease}>-</button>
<button onClick={onIncrease}>+</button>
</div>
</div>
);
};
ReactDOM.render(<App />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment