0

in my React App, after dispatching an action I want to rout to home page.

index.js

const store = createStore(
  rootReducer,
  composeWithDevTools(applyMiddleware(thunk))
);
ReactDOM.render(
  <BrowserRouter>
    <Provider store={store}>
      <App />
    </Provider>
  </BrowserRouter>,
  document.getElementById("root")
);

my component that I want to rout from.

class GameElements extends Component {
  cancel = () => {const { history, cancelGame: cancelGameAlt } = this.props;
    cancelGameAlt().then(() => history.push("/HomePage"));
  };

  render() {return( /*button that calls cancel*/);}

GameElements.propTypes = {
history: PropTypes.shape({
    push: PropTypes.func.isRequired
  }).isRequired}

and the cancelGame action is defined as

export const cancelGame = () => dispatch => {
  dispatch({ type: types.PLAYER_CANCELS_GAME });
};

the action is dispatched and the state at store changes, but I at this line; cancelGameAlt().then(() => history.push("/HomePage"));get an error Cannot read property 'then' of undefined

before clicking when page renders I get Failed prop type: The prophistoryis marked as required inGameElements, but its value isundefined. but I read that react-router 4 use the history as a prop of your component.

How should I route after calling an action?

Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123

0 Answers0