React and Redux: redirect after action

后端 未结 6 1357
孤城傲影
孤城傲影 2020-12-30 01:43

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

6条回答
  •  生来不讨喜
    2020-12-30 02:15

    The simplest solution

    You can use react-router-dom version *5+ it is actually built on top of react-router core.

    Usage:

    You need to import useHistory hook from react-router-dom and directly pass it in your actions creator function call.

    Import and Creating object

    import { useHistory } from "react-router-dom";
    
    const history = useHistory();
    

    Action Call in Component:

    dispatch(actionName(data, history));
    

    Action Creator Now, you can access history object as a function argument in action creator.

    function actionName(data, history) {}
    

提交回复
热议问题