我正在学习redux hooks,并想知道如何与redux saga一起使用。
目前saga中编写的代码如下。
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,
});
saga文件的定义如下。
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()
]);
}
那么问题是,
- 如果使用hooks,我们是否仍需要redux?
- 如何使用hooks重写上面的代码?