Redux & React Router: Combing dispatch and navigation (history.push)

Hiroki

I'm a bit clueless as to how to use React Router's history.push('/route') with Redux.

In other words, if you connect a component with Redux's mapDispatchToProps, how do you put history.push('/route') that is supposed to be fired after dispatching a certain action?

To clarify the issue, here is a snippet of my experimentation...

class PostContainer extends React.Component {

    handleSubmit(data) {
        return this.props.addPost(data);
    }

    render() {
        return (<Post onSubmit={data=> this.handleSubmit(data)}/>);
    }
}

const mapDispatchToProps = (dispatch) => {
    return {    
        addPost: (data) => {
            dispatch(addPost(data)).data.then((result) => {
                dispatch(addPostFulfilled(result.data));

                //How do you call the following function?
                this.props.history.push('/posts')
            }).catch((error) => {
                dispatch(addPostRejected());
            });
        },      
    }
}

export default connect(null, mapDispatchToProps)(PostContainer);

As you can see, as soon as a post is added into Redux's state through addPostFulfilled, the page needs to move to /posts via history.push.

According to Redux's doc, containers are made for updating states (as well as fetching data). So, dumb components are not supposed to put anything like history.push.

What would be a better approach than the code above?

Any advice will be appreciated.

Denis Rybalka

You can either user react-router-redux (require an initial set up) and dispatch your action from action creators

import { push } from 'react-router-redux'
class PostContainer extends React.Component {

  handleSubmit(data) {
    return this.props.addPost(data);
  }

  render() {
    return (
      <div>
        <Post onSubmit={data=> this.handleSubmit(data)}/>
      </div>
    );
  }
}

const mapDispatchToProps = (dispatch) => {
  return {    
    addPost: (data) => {
      dispatch(addPost(data)).data.then((result) => {
        dispatch(addPostFulfilled(result.data));

        //How do you call the following function?
        dispatch(push('/posts'));
      }).catch((error) => {
        dispatch(addPostRejected());
      });
    },      
  }
}
export default connect(null, mapDispatchToProps)(PostContainer);

or just pass push as a "done callback".

class PostContainer extends React.Component {

  handleSubmit(data, done) {
    return this.props.addPost(data, done);
  }

  render() {
    const { push } = this.props.history;
    return (
      <div>
        <Post onSubmit={data=> this.handleSubmit(data, push)}/>
      </div>
    );
  }
}

const mapDispatchToProps = (dispatch) => {
  return {    
    addPost: (data, done) => {
      dispatch(addPost(data)).data.then((result) => {
        dispatch(addPostFulfilled(result.data));

        //How do you call the following function?
        done('/posts')
      }).catch((error) => {
        dispatch(addPostRejected());
      });
    },      
  }
}

export default connect(null, mapDispatchToProps)(PostContainer);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React router history push always route to 404 page

react-router Redirect vs history.push

How to push to History in React Router v4?

Access to store.dispatch in a saga for use with react router redux

Redux connect "blocks" navigation with react-router-redux

react router is only replacing the final route on history.push

React Router BrowserRouter History Push & Google Analytics

React Router with Redux Loop - Reducers may not dispatch actions

React Router + Redux: history is undefined

How to test React-Router navigation through call to history.push using Jest?

React router `this.props.history.push` - history not defined

react router doesn't re-render after history push

Redux dispatch or react-navigation for passing params?

how to do history.push to another page after dispatch a signup success action in redux-thunk

React router this.props.history.push , pushing the location but not rendering the component

React router, codes after history.push run?

How to push to History in React Router v5?

history.push using react-router-dom

React router history.push is not adding path from root?

React Router + Redux - Dispatch an async action on route change?

Use Custom Async middleware to dispatch - Redux Thunk - react Router

React router history.push() not working from history.listen()

Onsubmit and history push is not working react router

React Router Switch Redux Dispatch

Combing React Router and Redux made my redirection KO

React router not working as expected when pass state to history.push

React router push to history and preserve relative path

React router history.push falls back to 404 route

Logout error on dispatch redux with React native navigation