location
ルートのコールバックから変数パラメーターを取得onEnter
し、アクションをストアにディスパッチできます。
上記の例を参照してください。
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import App from './App';
import { Route, Router, browserHistory } from 'react-router';
const store = createStore(rootReducer);
const routes = (
<Route
path="/location/:location"
component={App}
onEnter={handleEnter}
/>
);
function rootReducer(state = {
location: {},
}, action) {
switch (action.type) {
case 'ADD_TO_LOCATION':
return {
...state,
location: action.location,
};
default:
return state;
}
return state;
}
function handleEnter(nextState) {
// Map location data here.
// Next, we are dispatching mapped location to store.
store.dispatch({
type: 'ADD_TO_LOCATION',
location: nextState.params.location,
});
}
ReactDOM.render(<Router routes={routes} history={browserHistory} />, document.getElementById('root'));