-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
35 lines (28 loc) · 872 Bytes
/
store.ts
File metadata and controls
35 lines (28 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { configureStore, createSlice } from '@reduxjs/toolkit'
import { useSelector, connect } from 'react-redux'
const initialState = {
count: 0
}
export type StoreState = typeof initialState
const storeSlice = createSlice({
name: 'store',
initialState,
reducers: {
changeCount (state, { payload }: { payload: number }) {
state.count = payload
}
}
})
export const { changeCount } = storeSlice.actions
/** 封装一层 hook,添加准确类型提示 */
export function useTypeSelector<T = unknown> (fn: (state: StoreState) => T) {
return useSelector<StoreState, T>(fn)
}
/** 封装一层 hoc,给 mapStateToProps 里的 state 添加类型 */
export function typeConnect<T> (fn: (state: StoreState) => T) {
return connect<T, any, any, StoreState>(fn)
}
export default configureStore({
reducer: storeSlice.reducer,
devTools: true
})