1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

redux-persist の flow 型付け

Posted at

Action の型に Union しておきます。

src/types/index.js
import type { Action as _Action } from './action'
import type { State as _State } from './state'

type RehydrateAction = {
  type: 'persist/REHYDRATE',
  payload: _State,
}

export type State = _State
export type Action = _Action | RehydrateAction

reducer などで action.payload を State type として使えます。

src/containers/System/reducer.js
// @flow
import { REHYDRATE } from 'redux-persist/constants'

import type { Action, System } from '../../types'
import { Actions } from './actionTypes'

export type State = System

export const initialState: State = {
  selectedTab: 1,
}

export default function(state: State = initialState, action: Action): State {
  switch (action.type) {
    case Actions.SWITCH_TAB:
      return {
        ...state,
        selectedTab: action.target,
      }
    case 'persist/REHYDRATE':
      return action.payload.System

    default:
      return state
  }
}

akameco/s2s-examples のプロジェクト構成に導入する例にしています。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?