Redux Action Redirect

Hello everyone .we are working an application.After saving a new listing (post request) i want to go to another page.I am using redux .After action , i want to go to another page .Here is the some part of the link.Please let me know how can i solve this.


      const res = await fetch(`${SERVER_URL}/lists/add`, {
        method: 'POST',
        body: fd
      });

      /**
       * nature of Request and response status check.
       */
      if (res.status === 200) {
        const list = await res.json();
        toast.success('🎉 Your place has been added!');
        toast.success('🎉 You are redirecting to  preview your new listing!');
        dispatch({
          type: ADD_LIST_SUCCESS,
          payload:list
        })
        .then(()=>{
          history.push("listings")    //Here I want to Go to Listing Page .
        })
       
       
      } else {
        toast.error(' ⛔️ Some error has been occurred!!');
        dispatch({
          type: ADD_LIST_ERROR,
          payload: 'Some problem occurred'
        });
      }
    } catch (err) {
      console.log(err)
      toast.error(' ⛔️ Some error occured!');
      dispatch({
        type: ADD_LIST_ERROR,
        payload: err
      });
    }
  })();

The dispatch function is not a promise, so it doesn’t have a then method. Therefore you can either

  1. Update the history in the action creater
  2. Uodate the history in a reducer

How you do it will depend on if you’re using a routing library or not.

1 Like

Thanks a lot .I wrote a history.push(“path”) after the dispatch and it worked !

1 Like