import React, { createContext, useContext, useReducer } from "react"; import ReactDOM from "react-dom"; const CounterContext = createContext(); const reducer = (state, action) => { switch (action.type) { case "ADD_TO_COUNTER": { return { ...state, counter: state.counter + action.value }; } default: return state; } }; const initialState = { counter: 0 }; const CounterContextProvider = props => { const [state, dispatch] = useReducer(reducer, initialState); return ( {props.children} ); }; const Display = () => { const { state } = useContext(CounterContext); return

Valor del contador: {state.counter}

; }; const Increment = () => { const { dispatch } = useContext(CounterContext); return ( ); }; const Decrement = () => { const { dispatch } = useContext(CounterContext); return ( ); }; const App = () => { return ( ); }; ReactDOM.render(, document.getElementById("root"));