Created
May 29, 2019 11:00
-
-
Save ger86/7098afed21e18f80912f48b1877d4811 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { createContext, useContext } from "react"; | |
import ReactDOM from "react-dom"; | |
const CounterContext = createContext({ | |
counter: 0 | |
}); | |
const Display = () => { | |
const context = useContext(CounterContext); | |
return <h1>Valor del contador: {context.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 addToCounter = () => false; | |
const init = { counter: 0 }; | |
return ( | |
<CounterContext.Provider value={init}> | |
<Display /> | |
<Increment addToCounter={addToCounter} /> | |
<Decrement addToCounter={addToCounter} /> | |
</CounterContext.Provider> | |
); | |
}; | |
ReactDOM.render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment