#root
View Compiled
const {useReducer} = React;
const {render} = ReactDOM;

// Création de notre fonction reducer
const counterReducer = (state, action) => {
  switch (action) {
    case "increment":
      return state + 1;
    case "decrement":
      return state - 1;
    default:
      return state;
  }
};

const initialCounterValue = 0;

function Counter() {
  // Création du `useReducer` pour le compteur
  const [count, dispatchCounter] = useReducer(
    counterReducer,
    initialCounterValue
  );

  const increment = () => dispatchCounter("increment");
  const decrement = () => dispatchCounter("decrement");

  return (
    <div>
      {count}
      <button onClick={increment}> +1 </button>
      <button onClick={decrement}> -1 </button>
    </div>
  );
}

render(<Counter />, document.getElementById('root'))
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.8.7/polyfill.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js
  3. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js