Skip to content

Instantly share code, notes, and snippets.

@ger86
Last active May 29, 2019 10:47
Show Gist options
  • Save ger86/d4ca07914628230d5d5ce0a8791fe9e0 to your computer and use it in GitHub Desktop.
Save ger86/d4ca07914628230d5d5ce0a8791fe9e0 to your computer and use it in GitHub Desktop.
import React, { useState, useContext } from 'react';
import ReactDOM from 'react-dom';
const Display = ({ counter }) => <h1>Valor del contador: {counter}</h1>;
const Increment = ({ addToCounter }) => (
<button onClick={() => addToCounter(1)}>Sumar 1</button>
);
const Decrement = ({ addToCounter }) => (
<button onClick={() => addToCounter(-1)}>Restar 1</button>
);
const App = () => {
const [counter, setCounter] = React.useState(0);
const addToCounter = value => setCounter(counter + value);
return (
<>
<h1>useReducer</h1>
<Display counter={counter} />
<Increment addToCounter={addToCounter} />
<Decrement addToCounter={addToCounter} />
</>
);
};
ReactDOM.render(<App />, document.querySelector("#app"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment