Redux 工具包从 thunk 访问状态
Redux toolkit accessing state from thunk
我想在 thunk 中获取存储在状态中的值,在本例中为 projectId
。我可以在调用操作时访问此值而不将其作为参数传递吗?
interface RepoDetailsState {
openIssuesCount: number
error: string | null
}
const initialState: RepoDetailsState = {
openIssuesCount: -1,
error: null,
projectId: 'someProjectId'
}
const repoDetails = createSlice({
name: 'repoDetails',
initialState,
reducers: {
// ... reducers ...
}
})
export const {
getRepoDetailsSuccess,
getRepoDetailsFailed
} = repoDetails.actions
export default repoDetails.reducer
export const fetchIssuesCount = (
org: string,
repo: string
): AppThunk => async dispatch => {
//How do I access projectId from state?
}
您可以通过 getState
参数访问您的状态。
export const fetchIssuesCount = (
org: string,
repo: string
): AppThunk => async (dispatch, getState) => {
// Do something with state
getState()
}
也可以看到here。 (Ctrl + F getState 查看更多示例)
我想在 thunk 中获取存储在状态中的值,在本例中为 projectId
。我可以在调用操作时访问此值而不将其作为参数传递吗?
interface RepoDetailsState {
openIssuesCount: number
error: string | null
}
const initialState: RepoDetailsState = {
openIssuesCount: -1,
error: null,
projectId: 'someProjectId'
}
const repoDetails = createSlice({
name: 'repoDetails',
initialState,
reducers: {
// ... reducers ...
}
})
export const {
getRepoDetailsSuccess,
getRepoDetailsFailed
} = repoDetails.actions
export default repoDetails.reducer
export const fetchIssuesCount = (
org: string,
repo: string
): AppThunk => async dispatch => {
//How do I access projectId from state?
}
您可以通过 getState
参数访问您的状态。
export const fetchIssuesCount = (
org: string,
repo: string
): AppThunk => async (dispatch, getState) => {
// Do something with state
getState()
}
也可以看到here。 (Ctrl + F getState 查看更多示例)