<div id="root"></div>
// アクションの定義
const COUNT_INCREMENT = "count/increment";

// リデューサーの定義
const count = (state = 0, action) => {
  switch (action.type) {
    case COUNT_INCREMENT: {
      // 2. 値をインクリメントし、returnでストアを更新
      return state + 1;
      break;
    }
    default:
      return state;
  }
};

// ストアの作成
const store = Redux.createStore(Redux.combineReducers({ count }));

const Component = () => {
  const count = ReactRedux.useSelector(state => state.count)
  const dispatch = ReactRedux.useDispatch()
  
  return (
    <>
      {/* 3. ストアが更新されると、コンポーネントが際レンダリングされ、ビューを更新 */}
      <p>{count}</p>
      {/* 1. ボタンクリックでActionをDispatch(発行) */}
      <button onClick={() => dispatch({ type: COUNT_INCREMENT })}>
        click
      </button>
    </>
  );
};

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  //  ストアの変更を検知し、ビューイベント発火(レンダル)をするために対象のコンポーネントをラッピング 
  <ReactRedux.Provider store={store}>
    <Component />
  </ReactRedux.Provider>
);
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. //unpkg.com/react/umd/react.development.js
  2. //unpkg.com/react-dom/umd/react-dom.development.js
  3. https://cdnjs.cloudflare.com/ajax/libs/redux/4.2.0/redux.min.js
  4. https://cdnjs.cloudflare.com/ajax/libs/react-redux/8.0.2/react-redux.min.js