Posted at

redux-persist の flow 型付け

More than 1 year has passed since last update.

https://github.com/rt2zz/redux-persist

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 のプロジェクト構成に導入する例にしています。