use-saga-reducer
v3.0.0
Published
Use redux-saga without redux
Downloads
125
Maintainers
Readme
use-saga-reducer
Use sagas without redux! This library provides an abstraction over Redux Saga's External API.
Install
npm install use-saga-reducer
Usage
import useSagaReducer from 'use-saga-reducer'
import {takeLatest, call, put} from 'redux-saga/effects'
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 DataFetchingComponent: React.FC = () => {
const [state, dispatch] = useSagaReducer(dataFetchingSaga, reducer)
return (
<>
<pre>{state}</pre>
<button onClick={() => dispatch({type: 'FETCH'})}>Fetch Data</button>
</>
)
}