Response-router-redux does not refresh the page on location change

I have had no luck using the reductive version of the reactive router. When I add a product to my cart, this location changes, but the page doesn't refresh.

This is my setup. Everything else works with a new router adding items to the cart comes back from the REST server. However, using push

my actions in the file does not refresh the page.

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import createHistory from 'history/createBrowserHistory';
import { ConnectedRouter, routerMiddleware } from 'react-router-redux';
import reducers from './reducers';
import App from './components/app';

const history = createHistory();
const store = createStore(
    reducers,
    applyMiddleware(routerMiddleware(history), thunk)
);

ReactDOM.render(
    <Provider store={store}>
        <ConnectedRouter history={history}>
            <App />
        </ConnectedRouter>
    </Provider>,
    document.getElementById('app')
);

      

app.js

import React from 'react';
import { BrowserRouter as Router, Link, Route } from 'react-router-dom';

import Cart from '../containers/cart';
import Home from './home';
import Checkout from '../containers/checkout';
import Products from '../containers/products';


const App = () => (
    <Router>
        <div className="app">
            <Route exact={true} path="/" component={Home} />
            <Route path="/cart" component={Cart} />
            <Route path="/checkout" component={Checkout} />
            <Route path="/products/:id/:url_title" component={Products} />
        </div>
    </Router>
);

export default App;

      

./reducers

import { reducer as FormReducer } from 'redux-form';
import { combineReducers } from 'redux';
import { routerReducer } from 'react-router-redux';

const rootReducer = combineReducers({
    router: routerReducer,
    other: otherReducers
});

export default rootReducer;

      

./actions

import { push } from 'react-router-redux';

export function addProduct(props) {
    return dispatch =>
        axios.post(`${ROOT_URL}/api`, props, config)
            .then(response => {
                dispatch(push('/cart'));
            })
    }

      

+3


source to share


1 answer


Try connecting app.js

to reductions store and insert router

prop via mapStateToProps

. Even though you are not explicitly using a prop, your component should now re-render itself and its children whenever that support changes.



react-router

also suggests a withRouter

Higher Order Component to do something like this. However, I have had problems with it working in applications that use shouldComponentUpdate

multiple levels of the component tree.

0


source







All Articles