Connected-react-router: Params from match in redux store

Created on 27 Jun 2018  路  7Comments  路  Source: supasate/connected-react-router

I would love to use the params in the store form the router.
Now i only know the pathname and i need to do filtering to get the param i gave in the Route component.

Example:

// I only need "blogSlug" as a parameter. But i get the whole path.
<Route exact path="/item/:blogSlug" />

All 7 comments

@supasate

If you are using react-router v4 you can use the high order component withRouter as an alternative to enhance your component and have access to match params

@lluistfc thanks alot!
I've tried to make it work but i keep failing. I use some other things that maybe breaks the setup.

import {applyMiddleware, compose, createStore} from "redux";
import {connectRouter, routerMiddleware} from "connected-react-router";
import thunk from "redux-thunk";
import rootReducer from "./reducers";
import {createBrowserHistory} from "history";

export const history = createBrowserHistory();

export const store = createStore(
    connectRouter(history)(rootReducer),
    compose(
        applyMiddleware(
            thunk,
            routerMiddleware(history)
        )
    )
);

Can you help? I use react-redux, redux-thunk and connected-router.

The only difference i see between yours an my code is that you use routerMiddleware from connected-react-router and i'm using it from react-router-redux (v4.0.7).

I was taking a look at both middlewares from connected-react-router and react-router redux and the only difference is the store parameter in:
https://github.com/supasate/connected-react-router/blob/master/src/middleware.js#L8
Can you give it a try and see if that works for you?

React-router-redux is using the unsafe methods that are in >=16.3 given as a warning.
Everything works except for navigating and accessing the match match object.

Could I see some example? Right now i'm very busy and if I have time I could take a look at it maybe tonight or wednesday, but the idea is doing something like:
withRouter(connect(mapStateToProps)(Component)) and you can access location, match and history from your component props.
I haven't tried without a connected component. If this is your case I don't know if it will work but in the documentation there is an example with and without a connected component.

It works! I've wrapped the most upper component with "withRouter()".
Thanks a lot for all the help and thoughts.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dcs3spp picture dcs3spp  路  3Comments

zry754331875 picture zry754331875  路  3Comments

jjdp picture jjdp  路  4Comments

AdrienLemaire picture AdrienLemaire  路  5Comments

mjhoffm2 picture mjhoffm2  路  4Comments