動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About
SQL

Reducer

出典: Typescript×Reduxでカウンタアプリ作ってみる!各ソースコード / Reducer

Reducer (sql)#4d8b83057790
import {ActionNames, ActionType} from "../Actions/CounterAction";





export interface IState {

   num : number

}



const initialState : IState = {num: 0};



const CounterReducer = (state : IState = initialState , action : ActionType): IState => {

   switch (action.type) {

      case ActionNames.Increment:

         // 値が変わらない場合などはここでミスってる場合が多いから注意。 

         return {num : state.num + 1};

      case ActionNames.Decrement:

         return {num : state.num - 1};

      default:

         return state;

   }

};



export default CounterReducer;
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
  • id: #4d8b83057790
  • lines: 22
  • extracted: 2026-06-10

Source収録記事

この snippet は記事の「各ソースコード / Reducer」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。

同じ記事から

5
図鑑トップ