Я изучаю редукционные крючки и задаюсь вопросом, как их использовать вместе с сагой на редуксе.
В настоящее время код, написанный в саге, выглядит следующим образом.
centers.js
componentDidMount() {
this.props.getCenters();
}
...
<tbody>
{
this.props.centers ?
<React.Fragment>
{
centers.map((center, index) =>
<tr key={index}>
<td>{center.name}</td>
<td>{center.zip}</td>
</tr>
)
}
</React.Fragment>
:
<tr>
<td> No data available</td>
</tr>
}
</tbody>
Файл действий определяется следующим образом.
export const getCenters = () => ({
type: types.CENTERS_REQUEST,
});
Файл саги определяется следующим образом.
import { DEFAULT_ERROR_MSG } from '../../constants';
import { instance as centerProvider } from '../services/centerProvider';
function* fetchCenters() {
try {
const response = yield call(centerProvider.getCenters);
const centers = response.data.data.centers;
// dispatch a success action to the store
yield put({ type: types.CENTERS_SUCCESS, centers});
} catch (error) {
// dispatch a failure action to the store with the error
yield put(DEFAULT_ERROR_MSG);
}
}
export function* watchCenterRequest() {
yield takeLatest(types.CENTERS_REQUEST, fetchCenters);
}
export default function* centerSaga() {
yield all([
watchCenterRequest()
]);
}
Итак, вопрос в том,
- Нужен ли нам по-прежнему редукс, если мы используем хуки?
- Как мы можем переписать приведенный выше код с помощью хуков?