React-redux-starter-kit: state.location is null on page load

Created on 9 Jan 2017  路  5Comments  路  Source: davezuko/react-redux-starter-kit

When navigating directly to a subroute (first load of an app), state.location is null.

However, on navigating to a url in the app, the location is filled correctly:

screen shot 2017-01-09 at 3 15 42 pm

Is there a way to push this on page load ?

Most helpful comment

All 5 comments

I've temporarily solved this by updating store/location.js to include the following, but hopefully there's a more "correct" way:

BEFORE:

const initialState = null

AFTER
```
const getParams = query => {
if (!query) {
return { }
}

return (/^[?#]/.test(query) ? query.slice(1) : query)
.split('&')
.reduce((params, param) => {
let [ key, value ] = param.split('=')
params[key] = value ? decodeURIComponent(value.replace(/+/g, ' ')) : ''
return params
}, { })
}

// ------------------------------------
// Reducer
// ------------------------------------
const initialState = {
pathname: window.location.pathname,
search: window.location.search,
hash: window.location.hash,
action: null,
key: null,
query: getParams(window.location.search)
}
````

I am importing browserHistory in location.js and setting initialState like so:

// ------------------------------------
// Reducer
// ------------------------------------
const initialState = browserHistory.getCurrentLocation()
export default function locationReducer (state = initialState, action) {
  return action.type === LOCATION_CHANGE
    ? action.payload
    : state
}

@code-press - Yup, that works well.

Thanks!

Fixed with your PR, thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

macnibblet picture macnibblet  路  3Comments

ciokan picture ciokan  路  4Comments

nie-xin picture nie-xin  路  4Comments

orielz picture orielz  路  3Comments

glifchits picture glifchits  路  3Comments