반응형
환원기(저감-라우터)의 현재 위치에 액세스하는 방법?
환원기에 환원기를 넣은 상태에서 현재 위치의 현재 경로 및 쿼리를 얻는 방법은?mapStateToProps로 구성 요소 내에서 경로 이름을 쉽게 얻을 수 있지만, 현재 경로에 대한 Reducer에 액세스하고 싶다.Reducx-router 1.0.0-beta7 react-router 1.0.3을 사용하고 있다.
1.way - rendix-thunk 및 getState()를 통해 특정 동작이 포함된 패스 경로 이름
const someAction = data =>(dispatch,getState)=>{
dispatch({
type:'SOME_ACTION',
pathname:getState().router.pathname
})
}
2.way - 모든 동작으로 미들웨어 및 패스 경로 이름 작성
///write middleware
export const attachPathNameToAction = store => next => action=>{
action.pathname = store.getState().router.pathname //<-----passing pathname
next(action)
};
///then in reducer you allways can get pathname
case 'SOME_ACTION':
let pathname = action.pathname \\ <-------------
return {...state, action.data}
3.way - 구성 요소의 this.location.pathname에서 pass pathname을(통과 경로 이름)
//in component
let {pathname}= this.props.location;
this.props.someAction(data, pathname);
//workflow: component -> action -> reducer
참조URL: https://stackoverflow.com/questions/38084121/how-to-access-current-location-in-reducer-redux-router
반응형
'Programing' 카테고리의 다른 글
뉴라인 없이 인쇄(인쇄 'a'), 인쇄 공간, 제거 방법? (0) | 2022.03.23 |
---|---|
vue.js carousel-3d에 데이터가 표시되지 않음 (0) | 2022.03.23 |
현을 정수로 변환하는 방법? (0) | 2022.03.23 |
Python 3 ImportError: 'ConfigParser'라는 이름의 모듈 없음 (0) | 2022.03.23 |
전체 개체 대신 Vue 스토어 디스패치 단일 속성 (0) | 2022.03.23 |