'@redux-saga/core'的SagaIterator有什么用



SagaIterator在函数生成器中有什么用途?

import { SagaIterator } from '@redux-saga/core'
function* getCountry(action: ReturnType<typeof actions.country.request>) : SagaIterator {
try {       
const selectedCountry = (state: models.InitialStateTypes) => state.selectedCountryInitial        
const data = yield select(selectedCountry)       
const response: AxiosResponse<models.CountryInitialResponse> = yield call(
axios.get , 
'https://covid19.mathdro.id/api/countries/${data}' , 
{ 
params: action.payload 
}
);
yield put(actions.country.success(response.data))
} catch (error) {
yield put(actions.country.failure(error))
}
}

查看源代码,我们可以看到作者的评论:

/**
* Annotate return type of generators with `SagaIterator` to get strict
* type-checking of yielded effects.
*/
export type SagaIterator = IterableIterator<StrictEffect>

快速查看StrictEffect:的定义

export type StrictEffect<T = any> = SimpleEffect<T, any> | StrictCombinatorEffect<T>
export interface StrictCombinatorEffect<T> extends CombinatorEffect<T, StrictEffect<T>> {}
export interface SimpleEffect<T, P> {
'@@redux-saga/IO': true
combinator: false
type: T
payload: P
}

因此,它所说的是生成器生成redux传奇效果,如put、call、select等。这意味着,如果你试图在生成器中生成其他内容,你应该会从Typescript 中得到编译错误

相关内容

  • 没有找到相关文章

最新更新