looked at a lot of sample code of dva, including those recommended by dva website, and found a question: how to deal with the refresh of the list each time the data is processed, such as adding, deleting, or modifying user information? I think most of the examples are re-obtained from the server, which is obviously too inefficient. Some clients update state, but it doesn"t feel very useful:
*remove({ payload: id }, { call, put }) {
yield call(usersService.remove, id);
yield put({ type: "reload" });
},
*patch({ payload: { id, values } }, { call, put }) {
yield call(usersService.patch, id, values);
yield put({ type: "reload" });
},
*create({ payload: values }, { call, put }) {
yield call(usersService.create, values);
yield put({ type: "reload" });
},
*reload(action, { put, select }) {
const page = yield select(state => state.users.page);
yield put({ type: "fetch", payload: { page } });
},
do I think we should provide an automatic or semi-automatic ability to update client data with changed data? For example, after the server api executes successfully, you can use something like
effects: {
*remove({ payload: id }, { call, put }) {
const {data} = yield call(usersService.remove, id);
if (data && data.success) {
yield put({
type: "delSuccess",
payload
});
}
},
*Modify({ payload: values }, { call, put }) {
const {data} = yield call(usersService.modify,values);
if (data && data.success) {
yield put({
type: "modifySuccess",
payload
});
}
},
}
---
reducers: {
modifySuccess(state, action){
state.list.update(action.payload); //state
return {...state, loading: false};
},
delSuccess(state, action){
state.list.remove(action.payload); //state
return {...state, loading: false};
},