use-saga-reducer
Version:
Use redux-saga without redux
91 lines (90 loc) • 2.84 kB
TypeScript
import React, { Reducer, ReducerState, ReducerAction, Dispatch } from 'react';
import { Saga, RunSagaOptions } from 'redux-saga';
declare type SagaIOKeys = keyof Pick<RunSagaOptions<any, any>, 'channel' | 'dispatch' | 'getState'>;
declare type ExposedRunSagaOptions<A, S> = Omit<RunSagaOptions<A, S>, SagaIOKeys>;
interface SagaProdiderProps extends ExposedRunSagaOptions<any, any> {
}
/**
* Passes values into `runSaga` of each decendent `useSagaReducer` call.
*
* Methods are merged with local values, context methods are run first.
* Context value is merged with local value, local values will override
* existing properties.
* @param props Optional saga options
*/
export declare const SagaProvider: React.FC<SagaProdiderProps>;
/**
* Create an saga, disconnected from redux with its own state and dispatch.
*
* @see https://github.com/azmenak/use-saga-reducer
* @example
* ```
* function* dataFetcher() {
* try {
* const data = yield call(API.fetchData)
* yield put({type: 'FETCH_SUCCESS', payload: data})
* } catch (error) {
* yield put({type: 'FETCH_ERROR'})
* }
* }
*
* function* dataFetchingSaga() {
* yield takeLatest('FETCH', dataFetcher)
* }
*
* function reducer(state = {}, action) {
* if (action.type === 'FETCH_SUCCESS') {
* return action.payload
* }
*
* return state
* }
*
* //...
*
* const [state, dispatch] = useSagaReducer(saga, reducer)
* ```
*/
export declare function useSagaReducer<S extends Saga<never[]>, R extends Reducer<any, any>, I>(
/**
* Saga method, called when the component mounts, must be a generator function.
* Same as would be passed to reduxSaga.runSaga
*/
saga: S,
/**
* Reducer method, passed into React's `useReducer` method
*/
reducer: R,
/**
* Optional initalized argument, passed into React's `useReducer` method
*/
initializerArg?: I,
/**
* Store initialized function, passed into React's `useReducer` method
*/
initializer?: (arg: I) => ReducerState<R>,
/**
* Additional options passed into the `runSaga` method
*
* Supports:
* ```
* sagaMonitor // each monitor will run context methods then local methods
* onError // runs context method then local method
* context // merges context values into local values
* effectMiddlewares // combines with context middleswares, running context first
* ```
* @see https://redux-saga.js.org/docs/api/#runsagaoptions-saga-args
*/
runSagaOptions?: ExposedRunSagaOptions<any, S>): [ReducerState<R>, Dispatch<ReducerAction<R>>];
/**
* Helper function to create custom redux-saga effects
* @param type unique type string
* @param payload any object
*/
export declare function makeCustomEffect(type: string, payload: object): {
'@@redux-saga/custom': boolean;
combinator: boolean;
type: string;
payload: object;
};
export default useSagaReducer;