【问题标题】:Redux toolkit accessing state from thunkRedux 工具包从 thunk 访问状态
【发布时间】:2020-12-13 16:10:54
【问题描述】:

我想在 thunk 中获取存储在 state 中的值,在本例中为 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?
}

【问题讨论】:

    标签: javascript reactjs redux-toolkit


    【解决方案1】:

    您可以通过getState 参数访问您的状态。

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

    也可以看到here。 (Ctrl + F getState 查看更多示例)

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 2021-09-15
      • 2021-11-21
      • 1970-01-01
      • 2022-08-14
      • 2022-12-31
      • 2022-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多