本文整理匯總了TypeScript中typesafe-actions.createReducer函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript createReducer函數的具體用法?TypeScript createReducer怎麽用?TypeScript createReducer使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了createReducer函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: simulateBookmark
export const bookmarkReducer = createReducer<BookmarkState, ActionType<typeof bookmarkCreators>>(
INITIAL_STATE,
{
[getType(bookmarkCreators.copyBookmark)]: (
state: BookmarkState,
{payload}: ReturnType<typeof bookmarkCreators.copyBookmark>
) => {
return {
...state,
clipboard: {
id: payload.id,
isRemoveAfterPaste: false
}
}
},
[getType(bookmarkCreators.cutBookmark)]: (
state: BookmarkState,
{payload}: ReturnType<typeof bookmarkCreators.cutBookmark>
) => {
return {
...state,
clipboard: {
id: payload.id,
isRemoveAfterPaste: true
}
}
},
[getType(bookmarkCreators.getSearchResult)]: (
state: BookmarkState,
{payload}: ReturnType<typeof bookmarkCreators.getSearchResult>
) => {
return {
...state,
searchKeyword: payload.searchKeyword
}
},
[getType(bookmarkCreators.removeBookmarkTree)]: (
state: BookmarkState,
{payload}: ReturnType<typeof bookmarkCreators.removeBookmarkTree>
) => {
const removeFromIndex = state.trees.findIndex(R.pathEq(['parent', 'id'], payload.id))
if (removeFromIndex < 0) return state
return {
...state,
trees: state.trees.slice(0, removeFromIndex)
}
},
[getType(bookmarkCreators.removeDragIndicator)]: removeDragIndicator,
[getType(bookmarkCreators.removeNextBookmarkTrees)]: (
state: BookmarkState,
{payload}: ReturnType<typeof bookmarkCreators.removeNextBookmarkTrees>
) => {
const removeAfterIndex = state.trees.findIndex(
R.pathEq(['parent', 'id'], payload.removeAfterId)
)
if (removeAfterIndex < 0) return state
return {
...state,
trees: state.trees.slice(0, removeAfterIndex + 1)
}
},
[getType(bookmarkCreators.resetClipboard)]: (state: BookmarkState) => {
return {
...state,
clipboard: INITIAL_STATE.clipboard
}
},
[getType(bookmarkCreators.setBookmarkTrees)]: (
state: BookmarkState,
{payload}: ReturnType<typeof bookmarkCreators.setBookmarkTrees>
) => {
return {
...state,
trees: payload.bookmarkTrees
}
},
[getType(bookmarkCreators.setDragIndicator)]: (
state: BookmarkState,
{payload}: ReturnType<typeof bookmarkCreators.setDragIndicator>
) => {
const parentIndex = state.trees.findIndex((tree) => tree.parent.id === payload.parentId)
if (parentIndex === -1) return state
return R.over(
R.lensPath(['trees', parentIndex, 'children']),
R.insert(payload.index, simulateBookmark({type: CST.BOOKMARK_TYPES.DRAG_INDICATOR})),
removeDragIndicator(state)
)
}
}
)
示例2:
import {ActionType, createReducer} from 'typesafe-actions'
import {NAV_MODULE} from '../../constants'
import * as navigationCreators from './actions'
interface NavigationState {
selectedNavModule: NAV_MODULE
}
const INITIAL_STATE: NavigationState = {
selectedNavModule: NAV_MODULE.GENERAL
}
export const navigationReducer = createReducer<
NavigationState,
ActionType<typeof navigationCreators>
>(INITIAL_STATE).handleAction(navigationCreators.switchNavModule, (state, {payload}) => {
return {
...state,
selectedNavModule: payload.navModule
}
})
示例3: createAction
activeWindowIdQueue: ReadonlyArray<string>
}
export const windowsInitialState: WindowsState = {
activeWindowIdQueue: []
}
export const windowsCreators = {
setActiveWindowId: createAction('SET_ACTIVE_WINDOW_ID', (action) => (windowId: string) =>
action(windowId)),
unsetActiveWindowId: createAction('UNSET_ACTIVE_WINDOW_ID', (action) => (windowId: string) =>
action(windowId))
}
export const windowsReducer = createReducer<WindowsState, ActionType<typeof windowsCreators>>(
windowsInitialState
)
.handleAction(windowsCreators.setActiveWindowId, (state, {payload: windowId}) => {
const activeWindowIdQueue = [
...state.activeWindowIdQueue.filter((x) => x !== windowId),
windowId
]
return {
activeWindowId: activeWindowIdQueue[activeWindowIdQueue.length - 1],
activeWindowIdQueue
}
})
.handleAction(windowsCreators.unsetActiveWindowId, (state, {payload: windowId}) => {
const activeWindowIdQueue = state.activeWindowIdQueue.filter((x) => x !== windowId)
return {
activeWindowId: activeWindowIdQueue[activeWindowIdQueue.length - 1],
示例4:
positionLeft: 0,
positionTop: 0,
targetId: undefined
}
export const menuReducer = createReducer<MenuState, ActionType<typeof menuCreators>>(
INITIAL_STATE,
{
[getType(menuCreators.closeMenu)]: () => INITIAL_STATE,
[getType(menuCreators.openMenu)]: (
state: MenuState,
{payload}: ReturnType<typeof menuCreators.openMenu>
) => {
return {
...state,
...payload.coordinates,
targetId: payload.targetId
}
},
[getType(menuCreators.setMenuPattern)]: (
state: MenuState,
{payload}: ReturnType<typeof menuCreators.setMenuPattern>
) => {
return {
...state,
menuPattern: payload.menuPattern
}
}
}
)
示例5: createAction
export const keyBindingsPerWindowCreators = {
addEventListener: createAction(
'ADD_EVENT_LISTENER',
(action) => ({priority = 0, ...meta}: KeyBindingMeta, callback: KeyBindingEventCallback) =>
action({...meta, priority, callback})
),
removeEventListener: createAction(
'REMOVE_EVENT_LISTENER',
(action) => ({priority = 0, ...meta}: KeyBindingMeta, callback: KeyBindingEventCallback) =>
action({...meta, priority, callback})
)
}
export const keyBindingsPerWindowReducer = createReducer<
KeyBindingsPerWindowState,
ActionType<typeof keyBindingsPerWindowCreators>
>(keyBindingsPerWindowInitialState)
.handleAction(keyBindingsPerWindowCreators.addEventListener, (state, {payload}) => {
const {callback, key, priority, windowId} = payload
const keyBindings = state.get(windowId) || []
const updatedKeyBindings = [...keyBindings, {callback, key, priority}].sort(
(a, b) => a.priority - b.priority
)
return new Map(state).set(windowId, updatedKeyBindings)
})
.handleAction(keyBindingsPerWindowCreators.removeEventListener, (state, {payload}) => {
const {callback, key, priority, windowId} = payload
const keyBindings = state.get(windowId)
示例6: deleteFromMap
export const listsReducer = createReducer<ListsState, ActionType<typeof listsCreators>>(
listsInitialState,
{
[getType(listsCreators.removeList)]: (
state: ListsState,
{payload}: ReturnType<typeof listsCreators.removeList>
) => {
const {listIndex} = payload
return {
...state,
highlightedIndices: deleteFromMap(state.highlightedIndices, listIndex),
itemCounts: deleteFromMap(state.itemCounts, listIndex)
}
},
[getType(listsCreators.resetLists)]: () => listsInitialState,
[getType(listsCreators.setHighlightedIndex)]: (
state: ListsState,
{payload}: ReturnType<typeof listsCreators.setHighlightedIndex>
) => {
const {listIndex, itemIndex} = payload
return {
...state,
highlightedIndices: new Map(state.highlightedIndices).set(listIndex, itemIndex)
}
},
[getType(listsCreators.setItemCount)]: (
state: ListsState,
{payload}: ReturnType<typeof listsCreators.setItemCount>
) => {
const {listIndex, itemCount} = payload
return {...state, itemCounts: new Map(state.itemCounts).set(listIndex, itemCount)}
},
[getType(listsCreators.unsetHighlightedIndex)]: (
state: ListsState,
{payload}: ReturnType<typeof listsCreators.unsetHighlightedIndex>
) => {
const {listIndex, itemIndex} = payload
if (state.highlightedIndices.get(listIndex) !== itemIndex) {
return state
}
return {
...state,
highlightedIndices: deleteFromMap(state.highlightedIndices, listIndex)
}
}
}
)