简体   繁体   中英

Redux toolkit accessing state from thunk

I'd like to get a value stored in state in a thunk, in this case projectId . Can I access this value without passing it as a parameter when the action is called?.

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?
}

You can access your state via the getState parameter.

export const fetchIssuesCount = (
  org: string,
  repo: string
): AppThunk => async (dispatch, getState) => {
// Do something with state
 getState()
}

It can also be seen here . (Ctrl + F getState to see more examples)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM