Skip to content

Instantly share code, notes, and snippets.

@ger86
Created May 29, 2019 11:00
Show Gist options
  • Save ger86/7098afed21e18f80912f48b1877d4811 to your computer and use it in GitHub Desktop.
Save ger86/7098afed21e18f80912f48b1877d4811 to your computer and use it in GitHub Desktop.
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