简体   繁体   English

React.js:删除操作和刷新页面

[英]React.js: Delete operation and refresh page

I have implemented a delete operation.我已经实现了删除操作。 When this is done I make a redirect on the main page but I would like it to be automatically updated instead I have to update it again (calling a getEntities to get the values back)完成后,我在主页上进行重定向,但我希望它自动更新,而不是我必须再次更新它(调用 getEntities 来取回值)

reducer.ts减速器.ts

export const getEntities = createAsyncThunk('services/getEntities', async (param: parameters) => {
  const requestUrl = `${apiUrl}${param.sortParam ? `?page=${param.pageParam}&size=${20}&sort=${param.sortParam}` : ''}`;
  const response = await axios.get<any>(requestUrl);
  return response;
});

export const deleteEntity = createAsyncThunk('services/deleteEntity', async (id: any) => {
  const response = await axios.delete<any>(`${apiUrl}/${id}`);
  return response;
})

deletePage.tsx删除页面.tsx

const handleClose = () => {
        props.history.push('/service');
    };


    const confirmDelete = () => {
        dispatch(deleteEntity(property.entity.id))
            .then((resp: any) => {
                if (resp.payload.status === 204) {
                    handleClose();
                }
            })
    };

So in my "service" page I have to call getEntities again to get the updated list (without the deleted element).所以在我的“服务”页面中,我必须再次调用 getEntities 来获取更新的列表(没有删除的元素)。

In your opinion How can I do to avoid having to manually call getEntities after deletion?在您看来,我该怎么做才能避免在删除后手动调用 getEntities?

By your props I'm guessing you're using react-router .根据您的props ,我猜您正在使用react-router To refresh a route you would use history.go(0) .要刷新路线,您将使用history.go(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM