React 和 Redux:动作后重定向 [英] React and Redux: redirect after action

查看:57
本文介绍了React 和 Redux:动作后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 React/Redux 开发了一个网站,并使用 thunk 中间件来调用我的 API.我的问题涉及操作后的重定向.

I develop a website with React/Redux and I use a thunk middleware to call my API. My problem concerns redirections after actions.

我真的不知道如何以及在哪里进行重定向:在我的操作中,在减速器中,在我的组件中,......?

I really do not know how and where I can do the redirection: in my action, in the reducer, in my component, … ?

我的操作如下所示:

export function deleteItem(id) {
    return {
        [CALL_API]: {
            endpoint: `item/${id}`,
            method: 'DELETE',
            types: [DELETE_ITEM_REQUEST, DELETE_ITEM_SUCCESS, DELETE_ITEM_FAILURE]
        },
        id
    };
}

react-redux 已经在我的网站上实现了,我知道我可以做如下,但如果请求失败,我不想重定向使用:

react-redux is already implemented on my website and I know that I can do as below, but I do not want to redirect the use if the request failed:

router.push('/items');

谢谢!

推荐答案

通常更好的做法是像这样在组件中重定向:

Usually the better practice is to redirect in the component like this:

render(){
   if(requestFullfilled){
       router.push('/item')
   }
   else{
       return(
          <MyComponent />
       )
   }
}

这篇关于React 和 Redux:动作后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆