경로의 콜백 onEnter
에서 변수 매개 변수를 가져 와서 조치를 작업장에 전달할 수 있습니다.
위의 예를 참조하십시오 : 나는 componentWillReceiveProps 이벤트에 라우터 PARAMS 소요 루트 경로에 대한 컨테이너를 만든
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'));
작동합니다. 깨끗하고 완벽합니다. – haxpanel